polymorphism w/out signatures?
Simon Brunning
simon at brunningonline.net
Fri May 7 05:28:08 EDT 2004
"Delaney, Timothy C (Timothy)" <tdelaney at avaya.com> wrote in message news:<mailman.329.1083877186.25742.python-list at python.org>...
> A better approach anyway IMO for the above is to either always require
> an iterable, or to accept a variable number of parameters i.e.
>
> def nuke (lang, *langs): # must have at least one parameter
> nuke(lang1)
>
> for lang in langs
> nuke(lang)
>
> I could of course have constructed a combined tuple, but that's silly.
>
> Then you can call the above as any of:
>
> nuke('Perl')
> nuke('C++', 'Java', 'Perl')
> nuke(*('C++', 'Java', 'Perl'))
This is nice, Tim. But it only works if you don't need to pass in
other stuff, too. I've used an approach like this many times:
def setText(window, text, append=False):
# Ensure that text is a list
try:
text + ''
text = [text]
except TypeError:
pass
... rest of funtion, which can assume that 'text' is a list
I prefer not to use type() - it's enough that text is string-like or
list-like.
Cheers,
Simon B.
More information about the Python-list
mailing list