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 9228943
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:27:03+00:00 2026-06-18T05:27:03+00:00

I’ve found a behavior in Python that has baffled and irritated me and I

  • 0

I’ve found a behavior in Python that has baffled and irritated me and I was wondering what I got wrong.

I have a function which should take an arbitrary number of arguments and keywords, but in addition should have some default-valued keywords that comprise it’s actual interface:

def foo(my_keyword0 = None, my_keyword1 = 'default', *args, **kws):
    for argument in args:
        print argument

The problem is that if I try calling foo(1, 2, 3) I’ll only get the printout for 3 and the values 1 and 2 will override my keyword arguments.

On the other hand if I try moving my keywords after the *args or after the **kws it will cause a syntax error. The only solution I found to the problem was to extract the keyword arguments from **kws and setting default values to them:

def foo(*args, **kws):
    my_keyword0 = None if 'my_keyword0' not in kws else kws.pop('my_keyword0')
    my_keyword0 = 'default' if 'my_keyword1' not in kws else kws.pop('my_keyword1')
    for argument in args:
        print argument

This is horrible both because it forces me to add pointless code and because the function signature becomes harder to understand – you have to actually read the functions code rather than just look at its interface.

What am I missing? Isn’t there some better way to do this?

  • 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-18T05:27:04+00:00Added an answer on June 18, 2026 at 5:27 am

    Function arguments with default values are still positional arguments, and thus the result you see is correct. When you specify a default value for a parameter, you are not creating a keyword argument. Default values are simply used when the parameters are not provided by a function call.

    >>> def some_function(argument="Default"):
    ...     # argument can be set either using positional parameters or keywords
    ...     print argument
    ... 
    >>> some_function()    # argument not provided -> uses default value
    Default
    >>> some_function(5)    # argument provided, uses it
    5
    >>> some_function(argument=5)    # equivalent to the above one
    5
    >>> def some_function(argument="Default", *args):
    ...     print (argument, args)
    ... 
    >>> some_function()   #argument not provided, so uses the default and *args is empty
    ('Default', ())
    >>> some_function(5)   # argument provided, and thus uses it. *args are empty
    (5, ())
    >>> some_function(5, 1, 2, 3)   # argument provided, and thus uses it. *args not empty
    (5, (1, 2, 3))
    >>> some_function(1, 2, 3, argument=5)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: some_function() got multiple values for keyword argument 'argument'
    

    Note the last error message: as you can see the 1 got assigned to argument, and then python discovered the keyword referring to argument again, and thus raised an error.
    The *args are assigned only after assigning all possible positional arguments.

    In python2 there is no way to define a keyword-only value other than using **kwargs. As a workaround you could do something like:

    def my_function(a,b,c,d,*args, **kwargs):
        default_dict = {
            'my_keyword1': TheDefaultValue,
            'my_keyword2': TheDefaultValue2,
        }
        default_dict.update(kwargs)    #overwrite defaults if user provided them
        if not (set(default_dict) <= set('all', 'the', 'possible', 'keywords')):
            # if you want to do error checking on kwargs, even though at that
            # point why use kwargs at all?
            raise TypeError('Invalid keywords')
        keyword1 = default_dict['keyword1']
        # etc.
    

    In python3 you can define keyword-only arguments:

    def my_function(a,b,c,*args, keyword, only=True): pass
        # ... 
    

    Note that keyword-only does not imply that it should have a default value.

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

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have an array which has BIG numbers and small numbers in it. I
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into
I have an autohotkey script which looks up a word in a bilingual dictionary
I have a text area in my form which accepts all possible characters from
I need a function that will clean a strings' special characters. I do NOT

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.