Say, I have the following class in Python
class Foo(object):
a = None
b = None
c = None
def __init__(self, a = None, b = None, c = None):
self.a = a
self.b = b
self.c = c
Is there any way to simplify this process? Whenever I add a new member to class Foo, I’m forced to modify the constructor.
Please note that
sets a key-value pair in
Foo‘s dict:while
sets a key-value pair in the Foo instance object’s dict:
So setting the class members at the top of your definition is not directly related to the setting of the instance attributes in the lower half of your definition (inside the
__init__.Also, it is good to be aware that
__init__is Python’s initializer.__new__is the class constructor.If you are looking for a way to automatically add some instance attributes based on
__init__‘s arguments, you could use this:So when you say
you automatically get these instance attributes:
PS. Although I wrote this (for fun), I don’t recommend using
autoargsfor serious work. Being explicit is simple, clear and infallible. I can’t say the same forautoargs.PPS. Is it just me, or are a lot of buttons broken on Stackoverflow? The editor window has lost all its icons… 🙁Clearing the browser cache fixed the problem.