properties in Python 2.2

Blair Hall b.hall at irl.cri.nz
Thu May 23 23:39:39 EDT 2002


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'







More information about the Python-list mailing list