detecting param type

Jason Cunliffe jasonic at nomadicsltd.com
Wed Oct 25 22:34:33 EDT 2000


Alex

Thanks very much...

Alex Martelli <aleaxit at yahoo.com>
> 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!

This is very cool.

> 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...

You bet!.. it is just this kind of direct improvement to my simple code from
experienced pythoneers like yourself which makes me go aha!!.. and then
encourages me think more deeply about what is happening when classes and
methods are being instanced.

As I develop my methods for this module, I find I am adding more and more
keyword arguments with defaults and more detection clauses to act upon them,
such as:

def mymethod(index=0, display=0, sort='auto', event='something'): etc..

I like this because as I am experimenting in PythonWin interactive shell
where I can see my arguments appear inline and I find the code is easier to
maintain and more explicit. As things grow it seems like I should factor out
the common ones and instead put them into a superclass and thus make faster
changes later. For example if all my class methods have the same  set of 4
or 5 key arguments, should'nt these go outside in one place where they can
be improved.

So my question is do you have any advice/strategy for better ways to handle
lots of keyword arguments and class inheritance?

hope this makes sense

Thanks

Jason






More information about the Python-list mailing list