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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:45:55+00:00 2026-06-14T08:45:55+00:00

I am trying to implement a decorator class which would decorate methods in other

  • 0

I am trying to implement a decorator class which would decorate methods in other classes. However, I need the class which holds the decorated method available in the decorator. I can’t seem to find it anywhere.

Here’s an example:

class my_decorator(object):

  def __init__(self, arg1, arg2):
    print(self.__class__.__name__ + ".__init__")
    self.arg1 = arg1
    self.arg2 = arg2

  def __call__(self, my_callable):
    print(self.__class__.__name__ + ".__call__")
    print(type(my_callable))
    self.my_callable = my_callable
#    self.my_callable_method_class = ?where to get this?

    def function_wrapper(*args, **kwargs):
      print(self.__class__.__name__ + ".function_wrapper")
      print(self.arg1)
      self.my_callable.__call__(*args, **kwargs)
      print(self.arg2)

    return function_wrapper


class MyClass(object):

  @my_decorator(arg1="one", arg2="two")
  def decorated_method(self):
    print(self.__class__.__name__ + ".decorated_method")
    print(type(self.decorated_method))
    print("hello")


m = MyClass()
m.decorated_method()

That will print out this:

my_decorator.__init__
my_decorator.__call__
<type 'function'>
my_decorator.function_wrapper
one
MyClass.decorated_method
<type 'instancemethod'>
hello
two

In the decorator class the callable is of type function, while inside the class itself it is of type instancemethod. I can get the im_class from instancemethod, but there is no such thing in function.

How can I get the class containing the decorated method from within the decorator?

I could do this:

class my_decorator(object):

  def __init__(self, cls, arg1, arg2):

.
.

class MyClass(object):

  @my_decorator(cls=MyClass, arg1="one", arg2="two")
  def decorated_method(self):

.
.

But I would not like to do that because it’s redundant and not nice.

Or should I implement this some other way? I basicly need a couple of arguments to the decorator, and I need the class of the decorated method in the decorator.

  • 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-14T08:45:56+00:00Added an answer on June 14, 2026 at 8:45 am

    You could decorate the class:

    @decorate
    class MyClass(object):
    
      @my_decorator(arg1="one", arg2="two")
      def decorated_method(self):
    

    and use the outer decorator to send the class argument to the inner.


    None of your proposals can work, because they require access to the class before it exists. When you define a class, you first execute the code inside its body (defining functions etc) and then assign the resulting scope to the class as its __dict__. So at the time that decorated_method is defined, MyClass doesn’t exist yet.

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

Sidebar

Related Questions

I'm trying to implement a service contract that contains a method which takes a
As a learning exercise, I'm trying to implement a class which will emulate the
I trying to serialize a custom type which holds a dictionary among other members.
I'm attempting to implement a decorator on certain methods in a class so that
I'm trying to implement a decorator for custom error pages in web2py as per
I'm trying implement A* Start path finding in my games(which are written with JavaScript,
Trying to implement LoaderManager + CursorLoader. In onFinish method adapter should swap its cursor
i am trying to implement jquery's toggle method to make a dropdown menu, it
I'm trying to use Python's @property decorator on a dict in a class. The
I'm trying to implement a sitemesh decorator in my site. The example on their

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.