Skip to main content
5 of 8
added 16 characters in body
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 the class inherit from namedtuple instead?

MWB
  • 12.1k
  • 9
  • 50
  • 98