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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T08:06:21+00:00 2026-06-04T08:06:21+00:00

This is something that’s bugged me for awhile now: def test (*args, **kwargs): print

  • 0

This is something that’s bugged me for awhile now:

def test (*args, **kwargs):
    print target

test(foo='bar', target='baz')

I would presume that target='test' in the aFunc call at the bottom would end up in kwargs (and it does), and I would also presume that **would unpack kwargs in the function call, so target would exist as a keyword argument inside of aFunc. It doesn’t. I know that it comes in as a dict, but I need to have that dict unpack in the argument list. Is this possible? In short, is there any way to have *args and **kwargs disappear and have the actual args and kwargs go into the call?

Edit: I threw together a case where unpacking of *args and **kwargs might help:

Let’s say I have a function that prints a list:

def printList (inputList=None):
    print inputList

I want to be able to pass no list and have a default list supplied:

def ensureList (listFunc):
    def wrapper (inputList=None):
        listFunc(inputList=inputList or ['a','default','list'])
    return wrapper

@ensureList
def printList (inputList=None):
    print inputList

Now I want to get a bit more complicated with a list repeater:

@ensureList
def repeatList (inputList=None):
    print inputList*2

That works fine. But now I want variable repeating:

@ensureList
def repeatList (times, inputList=None):
    print inputList*times

Now you would be able to say:

repeatList(5)

It would generate the default list and repeat it 5 times.

This fails, of course, because wrapper can’t handle the times argument. I could of course do this:

@ensureList
def repeatList (inputList=None, times=1)

But then I always have to do this:

repeatList(times=5)

And maybe in some cases I want to enforce supplying a value, so a non-keyword arg makes sense.

When I first encountered problems like this last year, I thought a simple solution would be to remove the requirements on the wrapper:

def ensureList (listFunc):
    "info here re: operating on/requiring an inputList keyword arg"
    def wrapper (*args, **kwargs):
        listFunc(inputList=inputList or ['a','default','list'])
    return wrapper

That doesn’t work, though. This is why I’d like to have args and kwargs actually expand, or I’d like to have a way to do the expansion. Then whatever args and kwargs I supply, they actually fill in the arguments, and not a list and a dict. The documentation in the wrapper would explain requirements. If you pass in inputList, it would actually go in, and inputList in the call back to repeatList from the wrapper would be valid. If you didn’t pass in inputList, it would create it in the call back to repeatList with a default list. If your function didn’t care, but used *kwargs, it would just gracefully accept it without issue.

Apologies if any of the above is wrong (beyond the general concept). I typed it out in here, untested, and it’s very late.

  • 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-04T08:06:23+00:00Added an answer on June 4, 2026 at 8:06 am

    The answer to “why doesn’t ** unpack kwargs in function calls?” is: Because it’s a bad idea, the person who develop a function does not want local variable to just appear depending on the call arguments.

    So, this is not how it’s working and you surely do not want python to behave like that.

    To access the target variable in the function, you can either use:

    def test(target='<default-value>', *args, **kwargs):
        print target
    

    or

    def test(*args, **kwargs):
        target = kwargs.get('target', '<default-value>')
        print target
    

    However, if you want a hack (educational usage only) to unpack **kwargs, you can try that:

    def test(*args, **kwargs):
        for i in kwargs:
            exec('%s = %s' % (i, repr(kwargs[i])))
        print target
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a rewrite rule that looks like this: RewriteRule ^foo/bar/([\w]+)/files/([\S\s]+)$ /mydirectory/$1/$2 I'd like
This is something that's been bugging me for many years: why most online services
This is something that's I've wanted to know recently, mostly out of curiousity. I'm
This is something that I don't see much discussed. I'm developing a software that
This is something that I solved using reflection, but would like to see how
this is something that i don't want to program, but i was looking for
This is something that I have had trouble with in the past and it's
This is something that I'm sure I should know the answer to, but either
So this is something that I have not been able to find any documentation
hope to get some help here because this is something that really makes me

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.