By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,035 Members | 1,324 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,035 IT Pros & Developers. It's quick & easy.

Properties on old-style classes actually work?

P: n/a
Hello,

The python library docs read in section 2.1
(http://docs.python.org/lib/built-in-funcs.html):

"
....

property( [fget[, fset[, fdel[, doc]]]])
Return a property attribute for new-style classes (classes that
derive from object).

....
"
But in 2.4 at least properties also seem to work for old-style classes:

class O:

def __init__(self):
self._x = 15

def get_x(self):
return self._x

x = property(get_x)

o = O()
print o.x

outputs "15" as expected for the property.

Regards,
Paul
May 7 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Mon, 07 May 2007 10:44:35 +0200, Paul Melis wrote:
Hello,

The python library docs read in section 2.1
(http://docs.python.org/lib/built-in-funcs.html):

"
...

property( [fget[, fset[, fdel[, doc]]]])
Return a property attribute for new-style classes (classes that
derive from object).

...
"
But in 2.4 at least properties also seem to work for old-style classes:

Unfortunately, they don't -- they seem to work until you try assigning to
them. Here's the same property implemented with a new-style and old-style
class:
class New(object):
def __init__(self, s):
self._value = s
def upgetter(self):
return self._value.upper()
def upsetter(self, s):
self._value = s
value = property(upgetter, upsetter)

class Old:
def __init__(self, s):
self._value = s
def upgetter(self):
return self._value.upper()
def upsetter(self, s):
self._value = s
value = property(upgetter, upsetter)
Properties work with new-style classes:
>>obj = New('norwegian blue')
obj.value
'NORWEGIAN BLUE'
>>obj.value = 'nobody expects the spanish inquisition!'
obj.value
'NOBODY EXPECTS THE SPANISH INQUISITION!'
At first, they seem to work with old-style classes:
>>obj = Old('norwegian blue')
obj.value
'NORWEGIAN BLUE'

But problems occur once you try assigning to it:
>>obj.value = 'nobody expects the spanish inquisition!'
obj.value
'nobody expects the spanish inquisition!'

And now it is easy to see why:
>>obj.__dict__['value']
'nobody expects the spanish inquisition!'
>>obj.__dict__['_value']
'norwegian blue'

The call to assign obj.value over-rides the property with the raw value,
and from that moment on, obj.value is no longer a property, but just an
ordinary instance attribute.
--
Steven.
May 7 '07 #2

P: n/a
On May 7, 4:44 am, Paul Melis <p...@science.uva.nlwrote:
Hello,

The python library docs read in section 2.1
(http://docs.python.org/lib/built-in-funcs.html):

"
...

property( [fget[, fset[, fdel[, doc]]]])
Return a property attribute for new-style classes (classes that
derive from object).

...
"

But in 2.4 at least properties also seem to work for old-style classes:

class O:

def __init__(self):
self._x = 15

def get_x(self):
return self._x

x = property(get_x)

o = O()
print o.x

outputs "15" as expected for the property.

Regards,
Paul
Paul,

Sorry to dissapoint, but properties don't work in old style classes.
The 'get' property seems to work but as soon as you use the set
property it fails and even 'get' won't work after that. It surely is
deceiving, I wish it would just give an error or something. See
below.

-Nick Vatamaniuc

>>class O:
....: def __init__(self):
....: self._x=15
....: def get_x(self):
....: print "in O.get_x()"
....: return self._x
....: def set_x(self,newx):
....: print "in O.set_x(newx)"
....: self._x=newx
....: x=property(get_x, set_x)
....:
>>o=O()
>>o._x
15
>>o.x
in O.get_x()
in O.get_x()
15
>>o.x=42
>>#DANGER WILL ROBINSON, set_x NOT CALLED!!!
>>o.x
42
>>#HMM... properties ARE BROKEN FROM NOW ON
>>o._x
15
>>#...BROKEN INDEED
>>>
May 7 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.