Skip to main content
4 of 8
edited tags
MWB
  • 12.1k
  • 9
  • 50
  • 98

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