By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,345 Members | 1,783 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,345 IT Pros & Developers. It's quick & easy.

setting property to type other than what is given

P: n/a

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
Share this Question
Share on Google+
1 Reply


P: n/a
On 2004-05-08, Lee Harr <mi*****@frontiernet.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 discussion thread is closed

Replies have been disabled for this discussion.