Very, very strange problem with properties

  • Thread starter Kenneth McDonald
  • Start date
K

Kenneth McDonald

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
 
C

Carl Banks

Kenneth said:
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,
 
K

Kenneth McDonald

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
 

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

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top