Hi,
I am struggling to grasp this concept about def foo(*args).
The interactive interpreter is your friend! Try experimenting with it next time!
http://docs.python.org/tutorial/controlflow.html#arbitrary-argument-lists
That `def` defines a variadic function; i.e. a function which takes an
arbitrary number of positional arguments.
`args` will be a tuple of all the positional arguments passed to the function:.... print args
....(1, 2, 3)
If positional parameters precede the *-parameter, then they are
required and the *-parameter will receive any additional arguments:.... print 'a is', a
.... print 'b is', b
.... print 'args is', args
....Traceback (most recent call last):
a is 1
b is 2
args is ()a is 1
b is 2
args is (3,)a is 1
b is 2
args is (3, 4)
Also, what is def bar(*args, *kwargs)?
You meant: def bar(*args, **kwargs)
See
http://docs.python.org/tutorial/controlflow.html#keyword-arguments
Basically, the **-parameter is like the *-parameter, except for
keyword arguments instead of positional arguments.
Also, can the terms method and function be used interchangeably?
No. A method is function that is associated with an object (normally
via a class) and takes this object as its first argument (typically
named "self"). A function does not have any of these requirements.
Thus, all method are functions, but the reverse is not true.
(I'm ignoring complexities like classmethods and staticmethods for simplicity.)
Cheers,
Chris