473,569 Members | 2,772 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

setting property to type other than what is given


I am just starting to use properties, and wonder what you
might think of this ...
class RNumber(object) :
def __init__(self, n, nmin, nmax):
self.nmin = nmin
self.nmax = nmax
self.n = n

def _get_n(self):
return self._n

def _set_n(self, n):
if n == 'max':
self._n = self.nmax
elif n == 'min':
self._n = self.nmin
else:
self._n = min(self.nmax, n)
self._n = max(self.nmin, self._n)

n = property(_get_n , _set_n)

a = RNumber(5, 0, 10)
a.n 5 a.n = -3
a.n 0 a.n = 'max'
a.n 10 a.nmax = 50
a.n = 'max'
a.n

50

Jul 18 '05 #1
1 1432
On 2004-05-08, Lee Harr <mi*****@fronti ernet.net> wrote:

I am just starting to use properties, and wonder what you
might think of this ...
class RNumber(object) :
def __init__(self, n, nmin, nmax):
self.nmin = nmin
self.nmax = nmax
self.n = n

def _get_n(self):
return self._n

def _set_n(self, n):
if n == 'max':
self._n = self.nmax
elif n == 'min':
self._n = self.nmin
else:
self._n = min(self.nmax, n)
self._n = max(self.nmin, self._n)

n = property(_get_n , _set_n)

a = RNumber(5, 0, 10)
a.n 5 a.n = -3
a.n 0 a.n = 'max'
a.n 10 a.nmax = 50
a.n = 'max'
a.n 50


Could even go like this ...
class RNumber(object) :
def __init__(self, n, nmin, nmax):
self.nmin = nmin
self.nmax = nmax
self.n = n

def _get_n(self):
n = self._n
if n == 'max':
n = self.nmax
elif n == 'min':
n = self.nmin
return n

def _set_n(self, n):
if n == 'max':
self._n = n
elif n == 'min':
self._n = n
else:
self._n = min(self.nmax, n)
self._n = max(self.nmin, self._n)

n = property(_get_n , _set_n)

a = RNumber(5, 0, 10)
a.n = 'max'
a.n 10 a.nmax = 12
a.n

12

Jul 18 '05 #2

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

Similar topics

21
3946
by: Michael Bierman | last post by:
Please forgive the simplicy of this question. I have the following code which attempts to determine the color of some text and set other text to match that color. It works fine in Firefox, but does nothing in IE. I'd be greatful for any assistance. Also, if I will have problems the code on Opera or Safari, I'd appreciate any pointers--I don't...
18
18334
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 a remote update of tables and fields and can't find enough information on these things. Also, how do you index a field in code?
11
12737
by: Aaron Queenan | last post by:
Given the classes: class Class { public static implicit operator int(Class c) { return 0; } } class Holder
1
1978
by: Frank Yamrick | last post by:
I am trying to program an application that requires a large number of screnes that are very similar in the respect that all the bottons and labels interact with each other with the same relationship. If possible, it seems it would be easier to set up one standard page where the text is blank for each radiobutton, button, or label. The text...
5
4731
by: Dennis | last post by:
I have a class that inherits from DataGrid. I can set the rowheights in a DataGrid by tappig into the "get_Datagridrows" method. However, this does not work for classes that inherit from DataGrid. Anyone know how to do this on derived classes? I find it apalling that Microsoft provided a DataGrid that is so inflexible! -- Dennis in...
11
3477
by: Alexander Walker | last post by:
Hello I would like to write a method that allows me to pass a reference to an instance of a class, the name of a property of that class and a value to set that property to, the method would then set the property of the instance to the value here is an example of what the method might look like public void SetProperty(object instance,...
1
6462
by: laredotornado | last post by:
Hi, I'm using PHP 4.4.4 on Apache 2 on Fedora Core 5. PHP was installed using Apache's apxs and the php library was installed to /usr/local/php. However, when I set my "error_reporting" setting to be "E_ALL", notices are still not getting reported. The perms on my file are 664, with owner root and group root. The php.ini file is located...
3
6736
by: =?Utf-8?B?bWljaGFlbCBzb3JlbnM=?= | last post by:
I am wondering what is considered a best practice for setting column widths to fit the data for a DataGridView (.Net 2.0) that was created on-the-fly from an arbitrary query. What I would probably do is scan the width of each column for the first couple hundred or so rows and find the largest. (But how does one convert character counts to...
2
1493
balabaster
by: balabaster | last post by:
okay, I'm trying to reference a property within a class using a string "class.property", I've got a test class, imaginitively called "Test", hold your applause...it's got a property called value. I've got a function called GetValue that accepts an array of objects and a string for the property reference I want to get the value of. Now I can...
0
7921
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8118
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7666
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7964
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6278
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2107
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1208
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
936
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.