attributes of Python classes

Yermat loic at fejoz.net
Thu Mar 18 15:46:19 CET 2004


Terry Reedy a écrit :
>>>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

Ya right but then the documentation will not be very clear. I was 
thinking of two version of my class. Let say A that does not initialize 
its variable because it will be done later by the file reader. And B 
that inherit A and initialize instance variables.

In the documentation it will be easier to understand which one to use.

Just ideas...
Yermat




More information about the Python-list mailing list