12/19 On Beyond Strings : (*args) and (args)?


#1

Hi everyone!

Sorry but I think that I’ve missed something!
Why does it means to put an * before the argument of a fonction??
It appears on the first 2 but not the last one.

def biggest_number(*args):
print max(args)
return max(args)

def smallest_number(*args):
print min(args)
return min(args)

def distance_from_zero(arg):
print abs(arg)
return abs(arg)

biggest_number(-10, -5, 5, 10)
smallest_number(-10, -5, 5, 10)
distance_from_zero(-10)

Thank you (anyone who have time to answer) :slight_smile:

Lisiane


#2

*args is a special parameter for function, it gives you a tuple (immutable list). You can see this:

def biggest_number(*args):
    print args
    print max(args)
    return max(args)

biggest_number(-10, -5, 5, 10)

the function has only a single parameter, but multiple arguments at function call. Normally this wouldn’t work, but because of *args it does


#3

Ah OK!
Thank you very much stetim94!!


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.