Skip to content Skip to sidebar Skip to footer

Confusion In Understanding Tuple And *args In Python

I need a function which would be taking variadic arguments. The number of arguments may vary from 1 to N. def abc(*args): print 'ABC' print args print len(args) def ab

Solution 1:

Parentheses don't make tuples, commas do. To build a single-element tuple, the correct syntax is

tup = ("Hello123",)  # parentheses are optional but help readability

which is equivalent to

tup = "Hello123",

Remember that you can write

x, y = y, x  # swaps x and y using tuple packing/unpacking

just as well as

(x, y) = (y, x)

The only exception where parentheses are mandatory is the empty tuple ().

Post a Comment for "Confusion In Understanding Tuple And *args In Python"