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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T02:09:19+00:00 2026-05-20T02:09:19+00:00

I have the following descriptor, which saves the configuration inside my class after a

  • 0

I have the following descriptor, which saves the configuration inside my class after a method which is annotated with @saveconfig is called:

class saveconfig(object):
    def __init__(self, f):
        self.f = f

    def __get__(self, instance, owner):
        def wrapper(*args):
            self.f(instance, *args)
            instance.cfg.write()
            instance.paramcfg.write()
        return wrapper

It’s used like this:

class pbtools():
    @saveconfig
    def getip(self, ip):
        (...)

It works fine. Now i want to get the decorated method by using getattr. But since the method is wrapped by the descriptor, i only get wrapper as a result:

pbt = pbtools()
# results in "<function wrapper at 0x23cced8>:"
method = getattr(pbt, "getip")

How can i access the wrapped method getip with getattr, to be able to call it by it’s name? (of course i cannot access the method directly otherwise i would not have to do that).

Additional explanation:

The script is called from commandline. I have to map a command line parameter (string) to a method with the same name to invoke it. Furthermore, i have to map any additional parameters from the comamndline to parameters of the method, like this:

pbtools getip 192.168.178.123 #-> calls getip with parameter 192.168.178.123

Since i cannot get the original method, i don’t know how many parameters it has to map them. I have several methods that are decorated like that, because i want to move the cross cutting concern of saving the config out of the methods in the pbtools-class.

  • 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-05-20T02:09:20+00:00Added an answer on May 20, 2026 at 2:09 am

    I’m still not 100% sure I fully understand your problem. You say that “Since i cannot get the original method, i don’t know how many parameters it has to map them”, but you don’t need access to the original method to call by variable number of arguments (since the decorator has *args). You can do something like this:

    import sys
    cmd = sys.argv[1]
    params = sys.argv[2:]
    
    pbt = pbtools()
    
    func = getattr(pbt, cmd)
    func(*params)
    

    You can also simplify your decorator a bit. It doesn’t really need the __get__ mechanism.

    import functools
    
    def saveconfig(f):
        @functools.wraps(f)
        def wrapper(self, *args):
            f(self, *args)
            self.cfg.write()
            self.paramcfg.write()
        return wrapper

    The functools.wraps is a helper decorator that makes the wrapper mimic the original function (i.e. it copies the function name, docstring and stuff like that). It will make debugging easier, since you will know where exceptions are originating from etc.

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

Sidebar

Related Questions

I have the following method inside my repository class, to retrieve a Session object
I have a class with following description: public class Customer { public ISet<Client> Contacts
I have the following query: SELECT o.id,o.name FROM object o WHERE ( o.description LIKE
I have the following form which intercepts the ENTER key so that when the
I have Event model which contains following fields string name date start_at date end_at
I have a view which accepts the following model: Inherits=System.Web.Mvc.ViewPage<MVC_WordsByME.Models.JobCreationModel> This posts back to
I have the following XML file with page nodes which I want to read
I have the following Hibernate Mapping, which has to be mapped using the Table
I'm experimenting with the MVC beta 2 and have the following method in a
I have the following code which forks two new processes to take the contents

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.