7

I have the following code:

def foo(func, *args, named_arg = None):
    return func(*args)

returning a SyntaxError:

  File "tester3.py", line 3
    def foo(func, *args, named_arg = None):
                                 ^

Why is that? And is it possible to define somewhat a function in that way, which takes one argument (func), then a list of variable arguments args before named arguments? If not, what are my possibilities?

2
  • 1
    Works in Python 3; this is how you define keyword-only arguments which cannot be passed positionally. This syntax isn't available in Python 2. Commented Dec 11, 2012 at 14:31
  • One of my first questions here is related to this : (stackoverflow.com/questions/9872824/…) Commented Dec 11, 2012 at 14:40

4 Answers 4

10

The catch-all *args parameter must come after any explicit arguments:

def foo(func, named_arg=None, *args):

If you also add the catch-all **kw keywords parameter to a definition, then that has to come after the *args parameter:

def foo(func, named_arg=None, *args, **kw):

Mixing explicit keyword arguments and the catch-all *args argument does lead to unexpected behaviour; you cannot both use arbitrary positional arguments and explicitly name the keyword arguments you listed at the same time.

Any extra positionals beyond func are first used for named_arg which can also act as a positional argument:

>>> def foo(func, named_arg = None, *args):
...     print func, named_arg, args
... 
>>> foo(1, 2)
1 2 ()
>>> foo(1, named_arg=2)
1 2 ()
>>> foo(1, 3, named_arg=2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: foo() got multiple values for keyword argument 'named_arg'
>>> foo(1, 2, 3)
1 2 (3,)

This is because the any second positional argument to foo() will always be used for named_arg.

In Python 3, the *args parameter can be placed before the keyword arguments, but that has a new meaning. Normally, keyword parameters can be specified in the call signature as positional arguments (e.g. call your function as foo(somefunc, 'argument') would have 'argument' assigned to named_arg). By placing *args or a plain * in between the positional and the named arguments you exclude the named arguments from being used as positionals; calling foo(somefunc, 'argument') would raise an exception instead.

Sign up to request clarification or add additional context in comments.

1 Comment

when I do it like that and define a function as follows def func(text): print "Some text: ", text and call it via foo as foo(func,'world') I get the error TypeError: func() takes exactly 1 argument (0 given). And a call foo(func, named_arg = 1, "world") also does not work (SyntaxError: non-keyword arg after keyword arg).
2

No, Python 2 does not allow this syntax.

Your options are:

1) move the named arg to appear before *args:

def foo(func, named_arg = None, *args):
   ...

2) use **kwargs:

def foo(func, *args, **kwagrs):
   # extract named_arg from kwargs
   ...

3) upgrade to Python 3.

1 Comment

For (2) named_arg = kwargs.get('named_arg', None) is how to extract the named argument with a default value. This is likely the much better answer to the O.P. because solution (1) would confuse foo(f,bar) with foo(f,named_arg=bar).
1

change the order of the arguments to this:

def foo(func, named_arg = None, *args):
    return func(*args)

Comments

1

According to the docs, no:

Tutorial section 4.7.2 says:

In a function call, keyword arguments must follow positional arguments.

...and 4.7.3 says about 'Arbitrary argument lists':

Before the variable number of arguments, zero or more normal arguments may occur.

...so if you're going to use all three argument types, they need to be in the sequence

  1. Positional args
  2. Named args
  3. variable/arbitrary args

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.