I was wondering what was the best practice for initializing object attributes in Python, in the body of the class or inside the __init__ function?
i.e.
class A(object):
foo = None
vs
class A(object):
def __init__(self):
self.foo = None
If you want the attribute to be shared by all instances of the class, use a class attribute:
This causes
('foo',None)to be a(key,value)pair inA.__dict__.If you want the attribute to be customizable on a per-instance basis, use an instance attribute:
This causes
('foo',None)to be a(key,value)pair ina.__dict__wherea=A()is an instance ofA.