473,398 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

properties setting each other

mk
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?

Sep 3 '08 #1
4 1205
On Wed, 03 Sep 2008 15:57:50 +0200, mk wrote:
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:
Is there a way to achieve this goal of two mutually setting properties?
My attempt:
---
import math

class Square(object):
def __init__(self, val):
self._square = pow(val, 2)
self._value = math.sqrt(self.square)

def getsquare(self):
return self._square

def setsquare(self, square):
self._square = square
self._value = math.sqrt(self._square)

square = property(getsquare, setsquare)

def getvalue(self):
return self._value

def setvalue(self, value):
self._value = value
self._square = math.pow(value, 2)

value = property(getvalue, setvalue)
a = Square(5)
print a.square
print a.value
a.value = 10
print a.square
print a.value
a.square = 64
print a.square
print a.value
---

and the result:

$ python sqval.py
25
5.0
100.0
10
64
8.0
$

--
Regards,
Wojtek Walczak,
http://tosh.pl/gminick/
Sep 3 '08 #2
On Wed, 3 Sep 2008 14:31:17 +0000 (UTC), Wojtek Walczak wrote:
class Square(object):
def __init__(self, val):
self._square = pow(val, 2)
self._value = math.sqrt(self.square)
^^^^^^^^^^^^^^^^^^^^^^
or just:
self._value = val

:-)
--
Regards,
Wojtek Walczak,
http://tosh.pl/gminick/
Sep 3 '08 #3
mk schrieb:
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?
Better to make the getter for square return the square of value, and the
setter of square compute the root & set that. Like this:

class Squared2(object):

def __init__(self, value):
self.value = value
@apply
def squared():
def fset(self, squared):
self.value = math.sqrt(squared)

def fget(self):
return self.value ** 2

return property(**locals())

Diez
Sep 3 '08 #4
mk a écrit :
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)
the 'internal' prefix is already implied by the '_'. And your __init__
code is a useless duplication of fsetvalue, so just use the property and
get rid of copy-pasted code.

def fgetvalue(self):
return self._internalval
the '_' prefix already means 'internal'. The convention here would be to
name the attribute '_value' (to make clear it's the implementation
support for the 'value' property). Also, your property getter and setter
should also be marked as implementation using the '_' prefix - they are
implementation detail, not part of your class API.
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
Hem... Notice something here ?
self.value = math.sqrt(self.square)

square = property(fgetsquare, fsetsquare)
Your fsetsquare implementation is broken - it calls itself recursively.
You have to use different names for the property and the 'implementation
attribute' for the property. But even if you fix this, you'll have
another infinite recursion between the two setters.

The simplest solution : don't call one property from the other, do
direct attribute access within the setters:

import math

class Squared2(object):
def __init__(self, value):
self.value=value

def _fgetvalue(self):
return self._value
def _fsetvalue(self, value):
self._value=value
self._square=pow(value,2)
value = property(_fgetvalue, _fsetvalue)

def _fgetsquare(self):
return self._square
def _fsetsquare(self,square):
self._square = square
self._value = math.sqrt(square)
square = property(_fgetsquare, _fsetsquare)
Sep 3 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Dixie | last post by:
Can I set the Format property in a date/time field in code? Can I set the Input Mask in a date/time field in code? Can I set the Format of a Yes/No field to Checkbox in code? I am working on...
8
by: Chuck Bowling | last post by:
Is there any justification - from an OOP perspective - for wrapping an attribute in a Property beyond the ability to restrict access? private int myInt; public int MyInt { get { return...
5
by: rjl444 | last post by:
My app has a lot of properties, instead of placing the values in webconfig file (because I have tons), I would like to place these in it's own file. What is the best way of using an independant...
3
by: Patient Guy | last post by:
Subject line would seem to say it all: How does one trigger the execution of a method within an object or any other code/function with the setting of an object property? More elaboration for...
7
by: Ronald S. Cook | last post by:
In a .NET Windows app, if I set somehting like the title of the form to "MyApp" at run-time, will that make the app run slightly slower than if I had set the title at design-time? Thanks, Ron
0
by: =?Utf-8?B?UmljayBHbG9z?= | last post by:
For some unknown reason (user error?), I cannot get a NameValueCollection to persist in the app.config file. Unlike other settings, I cannot get the String Collection Editor GUI to allow my to...
6
by: | last post by:
I have made some user controls with custom properties. I can set those properties on instances of my user controls, and I have programmed my user control to do useful visual things in response to...
13
by: Dave | last post by:
When using the properties designer to store application wide properties how do you get this to work across a project group containing an EXE and a collection of DLLs. I'm using C#.Net 2005. I...
3
by: segecko | last post by:
Hi I have a created a custom usercontrol which inherites an Excel like usercontrol. In this usercontrol I have a custom property called SpreadTemplate, which is an enum with (at the moment) two...
2
by: Jan Eliasen | last post by:
Hi I am having some problems reading configuration values from a configuration file, using C# 2.0. I have programmed a Windows Service, and this part goes well - it runs nicely. Now, the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.