Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9014573
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:32:31+00:00 2026-06-16T03:32:31+00:00

I have the following code: def foo(func, *args, named_arg = None): return func(*args) returning

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T03:32:32+00:00Added an answer on June 16, 2026 at 3:32 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: def foo(*args) print len(args) print args now I'd like
I have the following code (minus some other operations): def foobar(): msg=None if foo:
I have following code: def whatever(url, data=None): req = urllib2.Request(url) res = urllib2.urlopen(req, data)
I have a following simple code: def get(): return [lambda: i for i in
Suppose I have following code: def foo(s): A dummy function foo. For example: >>>
Suppose I have following code def foo(x:Int):Unit = { if (x == 1) println
I have following code: class Foo(object): def __init__(self): baz=self.bar(10) @staticmethod def bar(n): if n==0:
I have the following code in a collection: class Author(Agent): def foo(self): self.find_another_document_and_update_it(ids) self.processed
I have the following code defining a type class. trait Foo[T] { def toFoo(x:
Suppose we have the following class hierarchy: class ClassA: @property def foo(self): return hello

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.