def withPositionalArgs(*args):
print args, type(args)
def withTupleAsArgument(tupleArg):
print tupleArg, type(tupleArg)
a=1
b=2
c=[10,20]
print withPositionalArgs(a,b,c)
print withTupleAsArgument(tuple([a,b,c]))
When I run this code:
(1, 2, [10, 20]) <type 'tuple'>
None
(1, 2, [10, 20]) <type 'tuple'>
None
Doubts:
As positional arguments are passed as a tuple, is there techcnially any difference between these 2 function calls? If I can already make a tuple at the time I’m calling, is there a need to use Positional arguments ? Things can work without them too, ain’t it ? Or is there something that I have not understood or ignored?
You need to ask yourself how your function will be used. Is it more natural to think of the arguments as an unrelated set of values, in which case positional arguments make more sense. Or do the values form a related group, in which case a tuple makes more sense.
You also need to consider how your function may be used. Suppose you have a function
that returns a tuple of values:
and you want to write a function
barwhose arguments are those values returned byfoo. Your two choices areHere are some ways you might call each of the two functions, using the return value of
foodirectly as your argument(s):So the choice between
bar1andbar2really depends on how you expect it to be used.