Very, very strange problem with properties

Kenneth McDonald kmmcdonald at wisc.edu
Tue May 4 00:01:22 EDT 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



More information about the Python-list mailing list