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.

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).

Advertisements

Thanks for the explanation. A follow up question: Suppose I want to
pass AT LEAST 2 positional arguments and AT LEAST 2 keyword arguments
into a function, Then I think I would logically define the function as
follows

Advertisements

If you want to have a varying number of positional arguments, and
require certain keyword arguments, I think you'll be forced to write it
in a clumsy way, similar to (untested):
def extract_args(funcname, mapping, *names):
for i in names:
try:
yield mapping
del mapping
except KeyError:
raise TypeError, (
"%s requires a keyword argument %s" %
(funcname, repr(i))))

When you write code like
def f(a, b=1)
Python will accept any of these calls to f:
f(0)
f(1, 2)
f(3, b=4)
f(a=5, b=6)
and even
f(b=7, a=8)
... the call that is relevant to your case is the second one. The
value in b can come from a positional argument or a keyword argument.
In your case, when you cakk 'f(1, 2, 3, karg=3)' karg gets two
values---one from the third positional argument, and one from the
keyword argument.

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!