Passing parameters to functions

Robert Brewer fumanchu at amor.org
Mon May 24 12:35:31 EDT 2004


Thomas Philips wrote:
> The following program passes parameters (inluding another function)
> into a function.
> 
> def f(myfunc,m=3,*numbers):
>     sumSquares=myfunc(*numbers[:m])
>     print sumSquares
> 
> 1. When I run it, Python gives me the following error message:
> Syntax error. There's an error in your program: *** non-keyword arg
> after keyword arg

The "keyword arg" in your function signature is the arg "m=3". Any args
appearing after it must also be given default values, which *numbers
doesn't do. My recommendation--just drop the default of 3 unless it's
critical.

def f(myfunc, m, *numbers)

> 2. f passes *arguments[:m] into sumofsquares. It appears to be passing
> a pointer. But Python doesn't have pointers

It doesn't have exposed pointers, but it does have references.
"arguments[:m]" creates a new list object; prepending "*" does a couple
of things:

1) It passes each item in that new list as an argument to your supplied
function, and
2) Within the sumofsquares scope, it rebinds that list to the name
"args".

You might want to reread http://effbot.org/zone/python-objects.htm:
"Assignment modify namespaces, not objects." In this context, calling
sumofsquares involves an 'assignment'; within the scope/namespace of the
function, the name "args" is bound to the objects being passed in (your
list).

Hope that helps!


Robert Brewer
MIS
Amor Ministries
fumanchu at amor.org




More information about the Python-list mailing list