Setting up properties from the __init__ method

Andrew Bennetts andrew-pythonlist at puzzling.org
Wed Oct 29 19:48:10 EST 2003


On Thu, Oct 30, 2003 at 12:22:09AM +0000, user at domain.invalid wrote:
> 
> So is saying self.attr in the __ini__ method different
> than just saying attr=value right in the class definition?

Yes; assigning to self.attr in a method (such as __init__) will set that
attribute on the instance, but assigning in the class definition will set
that attribute on the class.

> Assigning a the return of property() behaves clearly differently
> in each case.

Descriptors (such as properties) need to be set on a class to work their
magic.  Instances don't check their attributes for __get__/__set__/__del__
magic methods when accessing them, but classes do.

(This question pops up fairly regularly, and it's not obvious to new users
what's going on... perhaps there should be a FAQ entry about why
per-instance properties don't work?)

-Andrew.






More information about the Python-list mailing list