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

  • Home
  • SEARCH
  • 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 54179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T17:10:46+00:00 2026-05-10T17:10:46+00:00

I have a dict, which I need to pass key/values as keyword arguments.. For

  • 0

I have a dict, which I need to pass key/values as keyword arguments.. For example..

d_args = {'kw1': 'value1', 'kw2': 'value2'} example(**d_args) 

This works fine, but if there are values in the d_args dict that are not accepted by the example function, it obviously dies.. Say, if the example function is defined as def example(kw2):

This is a problem since I don’t control either the generation of the d_args, or the example function.. They both come from external modules, and example only accepts some of the keyword-arguments from the dict..

Ideally I would just do

parsed_kwargs = feedparser.parse(the_url) valid_kwargs = get_valid_kwargs(parsed_kwargs, valid_for = PyRSS2Gen.RSS2) PyRSS2Gen.RSS2(**valid_kwargs) 

I will probably just filter the dict, from a list of valid keyword-arguments, but I was wondering: Is there a way to programatically list the keyword arguments the a specific function takes?

  • 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. 2026-05-10T17:10:47+00:00Added an answer on May 10, 2026 at 5:10 pm

    A little nicer than inspecting the code object directly and working out the variables is to use the inspect module.

    >>> import inspect >>> def func(a,b,c=42, *args, **kwargs): pass >>> inspect.getargspec(func) (['a', 'b', 'c'], 'args', 'kwargs', (42,)) 

    If you want to know if its callable with a particular set of args, you need the args without a default already specified. These can be got by:

    def get_required_args(func):     args, varargs, varkw, defaults = inspect.getargspec(func)     if defaults:         args = args[:-len(defaults)]     return args   # *args and **kwargs are not required, so ignore them. 

    Then a function to tell what you are missing from your particular dict is:

    def missing_args(func, argdict):     return set(get_required_args(func)).difference(argdict) 

    Similarly, to check for invalid args, use:

    def invalid_args(func, argdict):     args, varargs, varkw, defaults = inspect.getargspec(func)     if varkw: return set()  # All accepted     return set(argdict) - set(args) 

    And so a full test if it is callable is :

    def is_callable_with_args(func, argdict):     return not missing_args(func, argdict) and not invalid_args(func, argdict) 

    (This is good only as far as python’s arg parsing. Any runtime checks for invalid values in kwargs obviously can’t be detected.)

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

Sidebar

Ask A Question

Stats

  • Questions 69k
  • Answers 69k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Yes - just explicitly provide the path to java.exe. For… May 11, 2026 at 12:40 pm
  • added an answer Sounds perfectly reasonable to me. By making it an inner… May 11, 2026 at 12:40 pm
  • added an answer Implement a custom trace listener: Let's see how we can… May 11, 2026 at 12:40 pm

Related Questions

I have a dict, that looks like this: { 'foo': { 'opt1': 1, 'opt2':
I have a web-service that I will be deploying to dev, staging and production.
I have a .Net desktop application with a TreeView as one of the UI
I have a Queue<T> object that I have initialised to a capacity of 2,
I have about 10million values that I need to put in some type of
This is a fairly trivial matter, but I'm curious to hear people's opinions on
If you have a controller method like so: @expose(json) def artists(self, action=view,artist_id=None): artists=session.query(model.Artist).all() return
I have an versioned document store which I want to access through an dict

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.