Skip to main content
2 of 8
Copy edited.
Peter Mortensen
  • 31.3k
  • 22
  • 109
  • 132

How do I avoid the "self.x = x; self.y = y; self.z = z" pattern in __init__?

I see patterns like

def __init__(self, x, y, z):
    ...
    self.x = x
    self.y = y
    self.z = z
    ...

quite frequently, often with a lot more parameters. Is there a good way to avoid this type of tedious repetitiveness? Should I inherit from namedtuple?

MWB
  • 12.1k
  • 9
  • 50
  • 98