attributes of Python classes

Terry Reedy tjreedy at udel.edu
Thu Mar 18 08:56:26 EST 2004


> > I agree that initializing all attributes in __init__ is a good idea.
> > Even if you initialize them to None and overwrite them in some other
> > method before ever accessing the value, it's still a nice way to help
> > soem future reader understand your class better.  If it's a good idea
to
> > have self-documenting code, I guess it's an even better idea to have
> > executable comments :-)

An alternative is to give attributes a default value of None as class
attributes.  This avoid time and space overhead of doing same on per
instance basis.  Instances only need instance-specific value when there is
one.

class C:
  a = None # what a is
  b = None # what b is

Terry J. Reedy









More information about the Python-list mailing list