Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

properties in Python 2.2

0 views
Skip to first unread message

Blair Hall

unread,
May 23, 2002, 11:39:39 PM5/23/02
to
I was trying to understand how to use the built-in property type
in Python 2.2. I seem to be missing something. So far, I
can't find any helpful hints in the Python doc either.

I am assuming that creating a property in a class allows one
to intercept setting and retrieving its value. Perhaps this is
just plain wrong!!??

If I define the following class

class test:
def fget(self):
print 'fget'
def fset(self,u):
print 'fset'

x = property(fget,fset)

Then create an instance: t = test()
I was expecting that
>>>t.x
would print 'fget', (which it does the first time, but not after the
line below)
and that
>>>t.x = 2
would print 'fset' (which it does not)

If I assign a new value to the property
>>> y = t.x
Then y is now a reference to the 'property'


Mark McEahern

unread,
May 24, 2002, 12:05:07 AM5/24/02
to
You need to subclass from object:

class foo(object):

...

cheers,

// mark
-

0 new messages