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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:02:26+00:00 2026-06-09T17:02:26+00:00

What I want to accomplish: dct = {‘foo’:0, ‘bar’:1, ‘baz’:2} def func(**dct): pass #function

  • 0

What I want to accomplish:

dct = {'foo':0, 'bar':1, 'baz':2}
def func(**dct):
    pass
#function signature is now func(foo=0, bar=1, baz=2)

However, the ** syntax is obviously clashing here between expanding a dict (what I want to do) and declaring a parameter that holds the keyword arguments (what I don’t want to do).

Is this possible?

  • 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-09T17:02:27+00:00Added an answer on June 9, 2026 at 5:02 pm

    Based on my interpretation of your requirements — you want to dynamically define a function with a signature that matches the content of adict provided at runtime — there are two issues here which makes it impractical.

    1. If the arguments are defined at run-time, how can your function reference the variables? Are you planning to build the function body at run-time as well?
    2. dicts are unordered, so you cannot reliably use them to define positional arguments

    I suspect this is an XY problem. If you can explain what you’re trying to achieve then perhaps we can be of better help.

    However, assuming you’re trying to assign default keyword arguments using a dict then one way to achieve this would be to use decorators. For example:

    def defaultArgs(default_kw):
        "decorator to assign default kwargs"
        def wrap(f):
            def wrapped_f(**kwargs):
                kw = {}
                kw.update(default_kw)  # apply defaults
                kw.update(kwargs)  # apply from input args
                f(**kw)  # run actual function with updated kwargs
            return wrapped_f
        return wrap
    

    You can then use this decorator to assign default keyword arguments to a function that expects only keyword arguments:

    defaults = {'foo':0, 'bar':1, 'baz':2}
    
    @defaultArgs(defaults)
    def func(**kwargs):
        print kwargs  # args accessible via the kwargs dict
    

    Results:

    func()  # prints {'baz': 2, 'foo': 0, 'bar': 1}
    func(foo=2)  # prints {'baz': 2, 'foo': 2, 'bar': 1}
    
    params = {'bar':1000, 'hello':'world'}
    func(**params)  # prints {'baz': 2, 'foo': 0, 'bar': 1000, 'hello': 'world'}
    

    Note that you will not be able to use positional arguments:

    func(1, 2, 3)   # raises TypeError
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to accomplish the following: def feed_items a = [] taggings.each do |k|
There are two things here i want to accomplish. List I want to pass
I have a problem with the function replace . What I want to accomplish
I want to accomplish this (mysql) query: UPDATE table SET field = field +
I want to accomplish a few tasks: Open a Word document through my program
I want to accomplish something like the following (my interest is in the toInt()
I want to accomplish nearly the same thing as this question , which is
What I want to accomplish is the effect I have seen in this site
What I want to accomplish: [a, b, c, d] -> [ (a, x), (b,
This question does pretty much what I want to accomplish, but my table is

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.