Properties on old-style classes actually work?

P

Paul Melis

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
 
S

Steven D'Aprano

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:
'NOBODY EXPECTS THE SPANISH INQUISITION!'


At first, they seem to work with old-style classes:
'NORWEGIAN BLUE'

But problems occur once you try assigning to it:
'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.
 
N

Nick Vatamaniuc

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



....: 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)
....:
in O.get_x()
in O.get_x()
15
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top