Tricky Areas in Python
Fredrik Lundh
fredrik at pythonware.com
Tue Oct 25 04:44:07 EDT 2005
Alex Martelli wrote:
>> my hard-won ignorance, and admit that I don't see the
>> problem with the property examples:
>>
>> > class Sic:
>> > def getFoo(self): ...
>> > def setFoo(self): ...
>> > foo = property(getFoo, setFoo)
>
> Sorry for skipping the 2nd argument to setFoo, that was accidental in my
> post. The problem here is: class Sic is "classic" ("legacy",
> "old-style") so property won't really work for it (the setter will NOT
> trigger when you assign to s.foo and s is an instance of Sic).
what's slightly confusing is that the getter works, at least until you attempt
to use the setter:
>>> class Sic:
... def getFoo(self):
... print "GET"
... return "FOO"
... def setFoo(self, value):
... print "SET", value
... foo = property(getFoo, setFoo)
...
>>> sic = Sic()
>>> print sic.foo
GET
FOO
>>> sic.foo = 10
>>> print sic.foo
10
(a "setter isn't part of an new-style object hierarchy" exception would have
been nice, I think...)
</F>
More information about the Python-list
mailing list