Adding properties to objects

Matthew Barnes matthew at barnes.net
Tue Apr 13 13:06:45 EDT 2004


Is it possible to add properties to objects dynamically?

I have an instance (x) of some new-style class (Foo), and I would like
to be able to do something like:

>>> x = Foo()
>>> x.myproperty = property(getsomething, setsomething, delsomething);
>>> x.myproperty        # invokes getsomething
>>> x.myproperty = 1    # invokes setsomething
>>> del x.myproperty    # invokes delsomething

However, when I evaluate x.myproperty I get back a property object
(which makes sense).  I get the feeling I'm missing a step to "bind"
the property object to the class instance.

Is this possible (it's Python... of course it's possible, right?), and
if so, how?

Matthew Barnes



More information about the Python-list mailing list