Wednesday, May 18, 2011

Function with arbitrary argument lists

This example shows how to define a function with arbitrary argument list.
def sum(*addends):
 s = 0
 for n in addends:
  s += n
 return s
So, now we can call the function with different numbers of argument:
print sum(1,2,3,4)
10
print sum(1,2,3)
6

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.