forcing a definition to be called on attribute change

Fredrik Lundh fredrik at pythonware.com
Tue Oct 24 05:52:23 EDT 2006


Michael Malinowski wrote:

> Apologies if this is a stupidly obvious or simple question. If I have a
> class with a series of attributes, is there a way to run a function
> definition in the class whenever a specific attribute is changed?

you can implement a __setattr__ hook, or, in Python 2.2 and newer, use
properties:

    http://www.python.org/doc/2.2.3/whatsnew/sect-rellinks.html#SECTION000340000000000000000

note that "setter" properties only work if you inherit from "object".

</F> 






More information about the Python-list mailing list