setting property to type other than what is given

L

Lee Harr

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)

50
 
L

Lee Harr

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)


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)

12
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top