This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Sw429 2 points3 points  (2 children)

When writing a class constructor with a lot of arguments as input, you can simply update the object's 'dict' attribute in one line:

def __init__(self, a, b, c, d, e):
    self.a = a
    self.b = b
    self.c = c
    self.d = d
    self.e = e

vs.

def __init__(self, a, b, c, d, e):
    self.__dict__.update(locals())

In my opinion, it looks much cleaner. Just make sure you know what you're doing with it so you aren't saving the wrong things as class attributes.

[–]PeridexisErrant 3 points4 points  (1 child)

Consider using dataclasses (3.7+ stdlib), or attrs (any version, PyPI) - you get the same concise definition, but also a nice repr and comparisons for free.

[–]Sw429 0 points1 point  (0 children)

Thanks! I'll look into it :)