argument type

Doug Holton a at b.c
Tue Dec 28 17:42:26 EST 2004


It's me wrote:
> The argument I wish to pass is either one string, or a list of strings, or a
> tuple of strings.
> 
> For instance, I have:
> 
>     def abc(arg1, arg2, arg3)
> 
> Let say that I expect arg1 and arg3 to be a number, and arg2 can be either
> one string, or a bunch of strings and I need to do something on each of the
> strings passed down.  


def seq(x):
	if hasattr(x,"__iter__"):
		return x
	else:
		return (x,)
		
def abc (arg1, arg2, arg3):
	for item in seq(arg2):
		print item
	
abc(1,"test1",2)
abc(1,["test1","test2"],2)
abc(1,2,3)
abc(1,[2,3,4],5)
abc(1,(2,3,4),5)



More information about the Python-list mailing list