[Tutor] constructors

Erik Price erikprice@mac.com
Thu, 11 Apr 2002 08:16:09 -0400


On Wednesday, April 10, 2002, at 09:22  PM, Kirby Urner wrote:

> You still need to provide a way to change class level defaults on
> a per instance basis, so if you don't provide this in the constructor,
> some other method will need to shoulder the burden.
>
> Class variables are shared across all instances, and may still be
> accessed even if self.property has been reset in a given instance,
> by referencing [the class].property, e.g.:
>
>   class Foo:
>       prop1 = "Test"
>       def __init__(self, prop1=None):
> 	if prop1:
> 	    self.prop1 = prop1

I see.  So a constructor is useful for individualizing an instance, 
whereas a class level default could set class level defaults (obviously) 
which will be the same across all instances unless another method 
changes them (which is what the constructor is sometimes used for).

Is this manipulation of class variables a side effect of how Python 
works, or is that desired behavior?  I haven't seen their use either 
(such as Classname.classvariablename) so I wonder if there is any reason 
to stay away from this kind of functionality.


Erik