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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:39:55+00:00 2026-05-29T09:39:55+00:00

Let’s assume I have a class to be used as a decorator like such:

  • 0

Let’s assume I have a class to be used as a decorator like such:

class specialmethod:
  def __init__(self,func):
     self.func = func

  def __call__(self,arg1,arg2):
     print arg1,arg2
     self.func(arg1,arg2)

But the method being decorated is within another class, that also must utilize self (the values within self are relevant to the function body post-decoration)

class A:
  @specialmethod
  def dosomething(self,data,otherdata):
    #this guy down here isn't getting the right value for self, an instance of A
    print type(self)

Traceback (most recent call last):
  File "deco.py", line 18, in <module>
    a.dosomething('foo','bar')
  File "deco.py", line 7, in __call__
    self.func(arg1,arg2)
TypeError: dosomething() takes exactly 3 arguments (2 given)

Is there anyway to preserve the way self would work if it were not decorated? I’ve tried passing it explicitly but it still expects the wrong number of parameters.

EDIT

I should clarify that the intended value for self in the final function body of dosomething should refer to the instance of A… not looking to get the decorator instance into the decorated function.

  • 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-29T09:39:56+00:00Added an answer on May 29, 2026 at 9:39 am

    When you use a class like a decorator, you have to realize that the normal magic that turns functions into methods (the descriptor protocol, specifically the __get__ method) won’t work for your class unless you explicitly implement it. You could do this manually, by implementing a __get__ method that returns a new type that remembers the instance it was retrieved from as well as the actual callable to call, but it’s a lot of work for very little gain.

    A more common approach is to not use an actual class as a decorator, and always have the decorator return an actual Python function. In this particular case, there isn’t really a point in keeping the class at all, so you could just do:

    def specialmethod(f):
        @functools.wraps(f)
        def wrapper(arg1, arg2):
            print arg1, arg2
            f(arg1, arg2)
        return wrapper
    

    … but if you really want to keep the class, you can do so by doing something like:

    class _specialmethod:
      def __init__(self,func):
         self.func = func
    
      def __call__(self,arg1,arg2):
         print arg1,arg2
         self.func(arg1,arg2)
    
    def specialmethod(f):
        wrapper_class = _specialmethod(f)
        @functools.wraps(f)
        def wrapper(arg1, arg2):
            wrapper_class(arg1, arg2)
        return wrapper
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's assume we have the following structure: index.php config.inc.php \ lib \ lib \
Let’s say I have a number like 0x448 . In binary this is 0100
Let's say on a page I have alot of this repeated: <div class=entry> <h4>Magic:</h4>
Let's assume that we are building a high traffic site that will be used
Let's say I have a text file composed like this ##### typeofthread1 ##### typeofthread2
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Let's say you have a class called Customer, which contains the following fields: UserName
Let's say I have a drive such as C:\ , and I want to
Let's say I have table with column 'URL' whrere I store urls like this
Let's imagine I have a Java class of the type: public class MyClass {

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.