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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:26:01+00:00 2026-06-17T12:26:01+00:00

following this topic: allow users to "extend" API functions class Inspector: def __init__(self, passedFunc):

  • 0

following this topic: allow users to "extend" API functions

class Inspector:
    def __init__(self, passedFunc):
        self.passedFunc = passedFunc

    def __call__(self, *args, **kwargs):
        # Inspector needs to generate a list of arguments of the passed function and print them in *correct* order

        # now how to generate a list of arguments using data from both ''args' and 'kwargs'?
        # the user might keep default argument values, in which case both will be None,
        # he might only use args, in which case it is easy,
        # but he might also use kwargs, in which case I don't see how to generate a list with correct order of arguments
        # and he might use all 3 from above together

        # I found this solution for when only default arguments are used
        if args == () and kwargs == {}: args = self.passedFunc.__defaults__

        print args  

@Inspector
def exampleFunc(value=0, otherValue=3):
    pass

exampleFunc()

How to generate a correct order args list for all scenarios?

  • 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-17T12:26:01+00:00Added an answer on June 17, 2026 at 12:26 pm

    This is how you can build the actual parameters list in the decorator:

    import inspect
    
    class Inspector:
        def __init__(self, passedFunc):
            self.passedFunc = passedFunc
    
        def __call__(self, *args, **kwargs):
    
            spec = inspect.getargspec(self.passedFunc)
            params = dict(zip(spec.args, args))
            defaults = dict(zip(spec.args[-len(spec.defaults):], spec.defaults))
    
            for k, v in kwargs.items():
                if k not in spec.args:
                    raise TypeError('unexpected argument', k)
                if k in params:
                    raise TypeError('mulitple values for argument', k)
                params[k] = v
    
            for k in spec.args:
                if k not in params:
                    if k in defaults:
                        params[k] = defaults[k]
                    else:
                        raise TypeError('missing argument', k)
    
            args_in_order = [params[x] for x in spec.args]
    
            print args_in_order
    

    Example:

    @Inspector
    def exampleFunc(value=0, otherValue=3):
        pass
    
    exampleFunc()  # 0,3
    exampleFunc('foo')  # foo,3
    exampleFunc(otherValue='foo', value=555) # 555,foo
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

following up with this topic: (Rails) How to display child records (one-to-many) in their
I use Struts2+JSP+Tomcat6+Hibernate+Mysql as my J2EE application framework.Following to this topic, I've had the
Referring to this topic: Minimize LINQ string token counter And using the following provided
Following on from my previous question on this topic, Postgres combining multiple Indexes :
Following this question and answer , I still have a bit trouble in the
Following this code my button works perfectly: http://twitter.github.com/bootstrap/javascript.html#modals Fades and everything. I want to
Following this discussion , I've encountered a bad access issue; A loop has several
following this link i was able to load and read pixels from a .gif.
Following this tutorial and running into trouble. [TestMethod] [ExpectedException(typeof(Exception))] public void VerifyPropertyNameMethod_NonExistentPropertyString_ThrowsException() { var
Following this thread, I tried to upload a file on the server. If I

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.