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:
def __init__(self, val):
self._internalv al=val
self.square=pow (self._internal val,2)
def fgetvalue(self) :
return self._internalv al
def fsetvalue(self, val):
self._internalv al=val
self.square=pow (self._internal val,2)
value = property(fgetva lue, fsetvalue)
def fgetsquare(self ):
return self.square
def fsetsquare(self ,s):
self.square = s
self.value = math.sqrt(self. square)
square = property(fgetsq uare, fsetsquare)
Traceback (most recent call last):
File "<pyshell#1 1>", line 1, in <module>
a=Squared2(5)
File "<pyshell#1 0>", line 5, in __init__
self.square=pow (self._internal val,2)
File "<pyshell#1 0>", line 19, in fsetsquare
self.square = s
File "<pyshell#1 0>", line 19, in fsetsquare
self.square = s
File "<pyshell#1 0>", line 19, in fsetsquare
self.square = s
File "<pyshell#1 0>", line 19, in fsetsquare
self.square = s
File "<pyshell#1 0>", line 19, in fsetsquare
self.square = s
File "<pyshell#1 0>", line 19, in fsetsquare
....
Is there a way to achieve this goal of two mutually setting properties?
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 ):
>>class Squared2(object ):
self._internalv al=val
self.square=pow (self._internal val,2)
def fgetvalue(self) :
return self._internalv al
def fsetvalue(self, val):
self._internalv al=val
self.square=pow (self._internal val,2)
value = property(fgetva lue, fsetvalue)
def fgetsquare(self ):
return self.square
def fsetsquare(self ,s):
self.square = s
self.value = math.sqrt(self. square)
square = property(fgetsq uare, fsetsquare)
>>a=Squared2( 5)
File "<pyshell#1 1>", line 1, in <module>
a=Squared2(5)
File "<pyshell#1 0>", line 5, in __init__
self.square=pow (self._internal val,2)
File "<pyshell#1 0>", line 19, in fsetsquare
self.square = s
File "<pyshell#1 0>", line 19, in fsetsquare
self.square = s
File "<pyshell#1 0>", line 19, in fsetsquare
self.square = s
File "<pyshell#1 0>", line 19, in fsetsquare
self.square = s
File "<pyshell#1 0>", line 19, in fsetsquare
self.square = s
File "<pyshell#1 0>", line 19, in fsetsquare
....
Is there a way to achieve this goal of two mutually setting properties?
Comment