dippim schrieb:
On Aug 14, 5:45 am, Jean-Michel Pichavant <jeanmic...@sequans.com>
wrote:
Emile van Sebille wrote:
On 8/13/2009 3:17 PM dippim said...
I am new to Python and I have a question about descriptors. If I have
a class as written below, is there a way to use descriptors to be
certain that the datetime in start is always before the one in end?
class foo(object):
def __init__(self,a = None,b = None)
self.start = a
self.end = b
from datetime import datetime
c = datetime(2009,8,13,6,15,0)
d = datetime(2009,8,14,12,0,0)
afoo = foo(c,d)
For instance, if the following code were run, I would like to instance
of foo to switch the start and end times.
afoo.start = datetime(2010,8,13,6,15,0)
I was thinking of using the __set__ descriptor to catch the assignment
and reverse the values if necessary,
why not...
class foo(object):
def __init__(self,a = None,b = None)
self.start = min(a,b)
self.end = max(a,b)
Emile
or
class foo(object):
def __init__(self, start, end)
self.start = start
self.end = end
Problem solved by design :o)
JM
Emile and JM,
Thanks for the response. However, these solution only work at
instantiation. If I change the value of start or end after
instantiation, then I can make start or end whatever I like without
regard to order.
But for that, you don't need descriptors. All you need is a property:
class Foo(object):
def __init__(self, start, end):
self._start = start
self._end = end
@property
def start(self):
return start
@start.setter
def start(self, value):
if value >= self._end:
raise ValueError, "Tried to set a value greater than end!"
self._start = value
# repeat for end, switch relational op
Diez
--
http://mail.python.org/mailman/listinfo/python-list