Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in Python by (12.7k points)

How can a variable number of an arguements need to be passed to a function. 

Along these lines to utilizing varargs in C or C++:

fn(a, b)

fn(a, b, c, d, ...)

1 Answer

0 votes
by (26.4k points)

Indeed. You can utilize *args as a non-keyword contention. You can then pass quite a few arguments. 

def manyArgs(*arg):

  print "I was called with", len(arg), "arguments:", arg

>>> manyArgs(1)

I was called with 1 arguments: (1,)

>>> manyArgs(1, 2, 3)

I was called with 3 arguments: (1, 2, 3)

As should be obvious, Python will unload the contentions as a solitary tuple with every one of the arguments.

Looking for a good python tutorial course? Join the python certification course and get certified.

For more details, do check out the below video tutorial...

Browse Categories

...