properties setting each other

mk mrkafk at gmail.com
Wed Sep 3 09:57:50 EDT 2008


Hello everyone,

I try to set two properties, "value" and "square" in the following code, 
and arrange it in such way that setting one property also sets another 
one and vice versa. But the code seems to get Python into infinite loop:

 >>> import math
 >>> class Squared2(object):

	def __init__(self, val):
		self._internalval=val
		self.square=pow(self._internalval,2)
		
	def fgetvalue(self):
		return self._internalval
		
	def fsetvalue(self, val):
		self._internalval=val
		self.square=pow(self._internalval,2)
		
	value = property(fgetvalue, fsetvalue)

	def fgetsquare(self):
		return self.square
	def fsetsquare(self,s):
		self.square = s
		self.value = math.sqrt(self.square)
		
	square = property(fgetsquare, fsetsquare)

	
 >>> a=Squared2(5)

Traceback (most recent call last):
   File "<pyshell#11>", line 1, in <module>
     a=Squared2(5)
   File "<pyshell#10>", line 5, in __init__
     self.square=pow(self._internalval,2)
   File "<pyshell#10>", line 19, in fsetsquare
     self.square = s
   File "<pyshell#10>", line 19, in fsetsquare
     self.square = s
   File "<pyshell#10>", line 19, in fsetsquare
     self.square = s
   File "<pyshell#10>", line 19, in fsetsquare
     self.square = s
   File "<pyshell#10>", line 19, in fsetsquare
     self.square = s
   File "<pyshell#10>", line 19, in fsetsquare

...

Is there a way to achieve this goal of two mutually setting properties?




More information about the Python-list mailing list