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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:54:00+00:00 2026-06-12T00:54:00+00:00

For this function def eat_dog(name, should_digest=True): print ate dog named %s. Digested, too? %

  • 0

For this function

def eat_dog(name, should_digest=True):
    print "ate dog named %s. Digested, too? %" % (name, str(should_digest))

I want to, external to the function, read its arguments and any default values attached. So for this specific example, I want to know that name has no default value (i.e. that it is a required argument) and that True is the default value for should_digest.

I’m aware of inspect.getargspec(), which does give me information about arguments and default values, but I see no connection between the two:

ArgSpec(args=['name', 'should_digest'], varargs=None, keywords=None, defaults=(True,))

From this output how can I tell that True (in the defaults tuple) is the default value for should_digest?

Additionally, I’m aware of the “ask for forgiveness” model of approaching a problem, but unfortunately output from that error won’t tell me the name of the missing argument:

>>> eat_dog()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: eat_dog() takes at least 1 argument (0 given)

To give context (why I want to do this), I’m exposing functions in a module over a JSON API. If the caller omits certain function arguments, I want to return a specific error that names the specific function argument that was omitted. If a client omits an argument, but there’s a default provided in the function signature, I want to use that default.

  • 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-12T00:54:01+00:00Added an answer on June 12, 2026 at 12:54 am

    Python3.x

    In a python3.x world, you should probably use a Signature object:

    import inspect
    
    def get_default_args(func):
        signature = inspect.signature(func)
        return {
            k: v.default
            for k, v in signature.parameters.items()
            if v.default is not inspect.Parameter.empty
        }
    

    Python2.x (old answer)

    The args/defaults can be combined as:

    import inspect
    a = inspect.getargspec(eat_dog)
    zip(a.args[-len(a.defaults):],a.defaults)
    

    Here a.args[-len(a.defaults):] are the arguments with defaults values and obviously a.defaults are the corresponding default values.

    You could even pass the output of zip to the dict constructor and create a mapping suitable for keyword unpacking.


    looking at the docs, this solution will only work on python2.6 or newer since I assume that inspect.getargspec returns a named tuple. Earlier versions returned a regular tuple, but it would be very easy to modify accordingly. Here’s a version which works with older (and newer) versions:

    import inspect
    def get_default_args(func):
        """
        returns a dictionary of arg_name:default_values for the input function
        """
        args, varargs, keywords, defaults = inspect.getargspec(func)
        return dict(zip(args[-len(defaults):], defaults))
    

    Come to think of it:

        return dict(zip(reversed(args), reversed(defaults)))
    

    would also work and may be more intuitive to some people.


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

Sidebar

Related Questions

consider this simple function def foo(l=[]): if not l: print List is empty else
Let's say I have this function: def print_a_line(line_count, f): print line_count, f.readline() I call
I have this function: def fun(arg1=1, arg2=2, arg3=3, arg4=4) Now if I want to
I am trying to this function: def sleep(sec): for i in range(sec): print(., end=
I'm trying to solve this newbie puzzle: I've created this function: def bucket_loop(htable, key):
I am writing a script and in my script I have this function: def
I'm using this simple function: def print_players(players): tot = 1 for p in players:
is there any way I can reproduce this ruby function: def Password.hash(password,salt) Digest::SHA512.hexdigest(#{password}:#{salt}) end
I have this function in Python: def Rotate_Vector(vector, axis, direction): where vector is a
For some reason this function confused me: def protocol(port): return port == 443 and

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.