Multiple arguments: how do you handle them 'nicely'?
jim at dsdd.org
Fri Aug 9 07:35:37 CEST 2002
On Thu, 08 Aug 2002 22:48:25 -0600, Blair Hall wrote:
> I would prefer that f() behave the same way for either a list or tuple,
> or a comma separated
> series of arguments. Moreover, if f() is passed something that emulates
> a sequence type then
> it should handle that too.
> How should I write f() so that it recognizes when it has been passed a
> that is sequence-like and when it simply has a series of arguments?
Personally, I would only allow one of these argument-passing conventions
in a single function - avoid unnescesary complexity like the plauge.
If you go the way of accepting a variable-length argument list (def
foo(*args)), remember that you can use the '*' notation in calls as well:
If you really want this (convenience for use at the command prompt, i'd
guess?), you simply want to see if you've got a single object is either
a tuple or a list:
if len(args)==1 and type(args) in (list,tuple):
args = list(args)
.. do stuff here ..
or something similar without whichever horrible errors i've made this
But really, don't do that. "simple." that is your mantra from now on,
every time you think about programming.
More information about the Python-list