473,407 Members | 2,320 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Properties on old-style classes actually work?

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
2 2265
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: \(\( Olivier \)\) | last post by:
Hello, I've searching for the old properties MergeCol et MergeRow from MSFlexGrid in the new WinForms DataGrid control. I would like to merge all the cells wich contains the same values. ...
21
by: VMI | last post by:
WHy are the get/set properties so useful? I know they're used so that I don't directly access a variable, but why is that so useful? What would the difference be between using it directly and using...
13
by: Dave | last post by:
When using the properties designer to store application wide properties how do you get this to work across a project group containing an EXE and a collection of DLLs. I'm using C#.Net 2005. I...
6
by: active | last post by:
I have an image and a graphics object created (FromImage) from that image. I need to create a new image and create a new graphics object from the new image. I want the new graphics object have...
26
by: optimistx | last post by:
A variable in global scope var a1 = 'contents of global variable a1'; can be references (with some limitations) as window; // or window.a1; // or even window;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.