Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Very, very strange problem with properties

Reply
Thread Tools

Very, very strange problem with properties

 
 
Kenneth McDonald
Guest
Posts: n/a
 
      05-04-2004
For some reason, properties seem to have stopped working correctly
on my system. (Mac OS X 10.3, custom-compiled Python 2.3.3) To the
best of my knowledge, they were working correctly at some point
in the not-too-distant past. The confusing thing is that they
_partly_ work.

Consider the following code:

class foo:
def __init__(self):
self._x = 3

def __setx(self, val):
print "Calling __setx"
pass

x = property(fget=lambda self: self._x, fset=__setx)

f = foo()
print f.__dict__
print f.x
f.x = 2
print f.x
f._x = 7
print f.x
print f.__dict__


Running this _should_ print (ignoring the dict printouts)
3
Calling __setx
3
7
since the __setx function does nothing. In fact, what I
get (dicts included) is:

{'_x': 3}
3
2
2
{'x': 2, '_x': 7}

i.e. the assignment f.x = 2 is overwriting the property!
If I define the property as

x = property(fget=lambda self: self._x)

then running the code should cause an error on f.x = 7;
in fact, I get the same result as when the fset is
defined as __setx.

So, it seems that the "getter" aspect of properties is
working, but the "setter" aspect is ignored in such a way
that regular attribute assignment takes place. I find
this very, very strange.

I would prefer to avoid reinstalling Python on my system,
since keeping a custom Python working on OS X is a bit
of a gamble anyway, at this point, but will if I have to.
But I would prefer it if someone could suggest other
alternatives to try first, and frankly, I'm also just
curious as to what could cause an oddity like this.


Thanks,
Ken McDonald
 
Reply With Quote
 
 
 
 
Carl Banks
Guest
Posts: n/a
 
      05-04-2004
Kenneth McDonald wrote:
>
>
> For some reason, properties seem to have stopped working correctly
> on my system. (Mac OS X 10.3, custom-compiled Python 2.3.3) To the
> best of my knowledge, they were working correctly at some point
> in the not-too-distant past. The confusing thing is that they
> _partly_ work.
>
> Consider the following code:
>
> class foo:



There's your problem. You've defined a classic class. Properties
only work with new-style classes, so you need to use "class
foo(object)".


suspects-he-will-have-realized-this-2-seconds-after-posting-ly yr's,


--
CARL BANKS http://www.aerojockey.com/software
"If you believe in yourself, drink your school, stay on drugs, and
don't do milk, you can get work."
-- Parody of Mr. T from a Robert Smigel Cartoon
 
Reply With Quote
 
 
 
 
Kenneth McDonald
Guest
Posts: n/a
 
      05-04-2004
Urgh, and that explains why it was working before: I changed my class's
parent class from 'object' to another classic class. Sigh.

Still, I find it kinda strange that the getter worked...


Thanks for the help!
Ken

In article <LHElc.13152$(E-Mail Removed)>, Carl Banks wrote:
> Kenneth McDonald wrote:
>>
>>
>> For some reason, properties seem to have stopped working correctly
>> on my system. (Mac OS X 10.3, custom-compiled Python 2.3.3) To the
>> best of my knowledge, they were working correctly at some point
>> in the not-too-distant past. The confusing thing is that they
>> _partly_ work.
>>
>> Consider the following code:
>>
>> class foo:

>
>
> There's your problem. You've defined a classic class. Properties
> only work with new-style classes, so you need to use "class
> foo(object)".
>
>
> suspects-he-will-have-realized-this-2-seconds-after-posting-ly yr's,
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
very very very long integer shanx__=|;- C Programming 19 10-19-2004 03:55 PM
very very very long integer Abhishek Jha C Programming 4 10-17-2004 08:19 AM
Quick Book file access very very very slow Thomas Reed Computer Support 7 04-09-2004 08:09 PM
Re: very very strange C++ NaN or memory management related problem brewman C++ 0 08-28-2003 10:35 PM
very Very VERY dumb Question About The new Set( ) 's Raymond Arthur St. Marie II of III Python 4 07-27-2003 12:09 AM



Advertisments