detecting param type
Alex Martelli
aleaxit at yahoo.com
Wed Oct 25 18:08:26 EDT 2000
"Jason Cunliffe" <jasonic at nomadicsltd.com> wrote in message
news:sve9sa9p0rot59 at corp.supernews.com...
[snip]
> def append(self, date=now(), entry='entry', display=0):
> s = self.eventdict
> # detect if date param is already a string or a datetime object
> if type(date) == 'DateTime': ### <<< this is the one I need help
> with
> date = str(date)
> s.append(date, entry)
> if display:
> self.display()
I would simply change the routine to:
def append(self, date=now(), entry='entry', display=0):
self.eventdict.append(str(date), entry)
if display:
self.display()
It does no harm to call str(date) 'again' even if date
is _already_ a string -- in that case it will just be a
very fast no-operation, probably faster than checking
for specific types!
One warning: the default value for the date parameter
is computed ONCE -- i.e., the now() function is called
just once, when the routine is defined. If that is not
what you desire, you must take a slightly different tack:
def append(self, date=None, entry='entry', display=0):
if not date: date = now()
self.eventdict.append(str(date), entry)
if display:
self.display()
this way, now() is called afresh each time if needed.
Just thought this could help...
Alex
More information about the Python-list
mailing list