What does *args mean?


#1

I was reading one of the answered questions and I copied the code to ask a question.

1 def smallest_number(*args):
2 print min(args)
3 return min(args)
4
5 def distance_from_zero(arg):
6 print abs(arg)
7 return abs(arg)

On line 1 it says: def smallest_number(*args)
What role does the asterisk achieve in *args ? Rather than arg on line 5.


#2

splat! args means we have an unknown number of arguments accepted by the function. It will likely mean one or more arguments.

def smallest(*args):
    try:
        print (min(args))
    except ValueError:
        print ("min() arg is an empty sequence")

smallest()
smallest(1)
smallest(3,4,5,6,7,8,9)
min() arg is an empty sequence
1
3

#3

@alankwtam26
and as expansion of @mtf answer
http://stackoverflow.com/questions/3394835/args-and-kwargs


#4

Thank you very much.