Multiple arguments: how do you handle them 'nicely'?

Jim Meier 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
> container
> 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:
foo(*some_func_returning_a_tuple())

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:

def foo(*args):
	if len(args)==1 and type(args[0]) in (list,tuple):
		args = list(args[0])
	.. do stuff here ..

or something similar without whichever horrible errors i've made this
time.


But really, don't do that. "simple." that is your mantra from now on,
every time you think about programming.


-Jim
	



More information about the Python-list mailing list