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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:04:35+00:00 2026-06-12T13:04:35+00:00

When you decorate a method, it is not bound yet to the class, and

  • 0

When you decorate a method, it is not bound yet to the class, and therefor doesn’t have the im_class attribute yet. I looking for a way to get the information about the class inside the decorator. I tried this:

import types

def decorator(method):

    def set_signal(self, name, value):
        print name
        if name == 'im_class':
            print "I got the class"

    method.__setattr__ = types.MethodType(set_signal, method)

    return method


class Test(object):
    @decorator
    def bar(self, foo):
        print foo

But it doesn’t print anything.

I can imagine doing this:

class Test(object):
    @decorator(klass=Test)
    def bar(self, foo):
        print foo

But if I can avoid it, it would make my day.

  • 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-12T13:04:36+00:00Added an answer on June 12, 2026 at 1:04 pm

    __setattr__ is only called on explicit object.attribute = assignments; building a class does not use attribute assignment but builds a dictionary (Test.__dict__) instead.

    To access the class you have a few different options though:

    1. Use a class decorator instead; it’ll be passed the completed class after building it, you could decorate individual methods on that class by replacing them (decorated) in the class. You could use a combination of a function decorator and a class decorator to mark which methods are to be decorated:

      def methoddecoratormarker(func):
          func._decorate_me = True
          return func
      
      def realmethoddecorator(func):
          # do something with func. 
          # Note: it is still an unbound function here, not a method!
          return func
      
      def classdecorator(klass):
          for name, item in klass.__dict__.iteritems():
              if getattr(item, '_decorate_me', False):
                  klass.__dict__[name] = realmethoddecorator(item)
      

      You could use a metaclass instead of a class decorator to achieve the same, of course.

    2. Cheat, and use sys._getframe() to retrieve the class from the calling frame:

      import sys
      
      def methoddecorator(func):
           callingframe = sys._getframe(1)
           classname = callingframe.f_code.co_name
      

      Note that all you can retrieve is the name of the class; the class itself is still being built at this time. You can add items to callingframe.f_locals (a mapping) and they’ll be made part of the new class object.

    3. Access self whenever the method is called. self is a reference to the instance after all, and self.__class__ is going to be, at the very least, a sub-class of the original class the function was defined in.

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

Sidebar

Related Questions

I am attempting to decorate a method inside a class but python is throwing
Goal: Make it possible to decorate class methods. When a class method gets decorated,
I have a method in my class that accepts an object by reference. It
OK, I allready have read all questions about class method decorating, but my case
Is there a way to decorate a method that will do some logging, then
i have a type class Foo { public string Name { get; set; }
I have an abstract class with a single concrete method. In this method I
I have class Window. This class has method onClick which gets argument the id
I would like to be able to decorate any method with a custom Trace
I have a Silverlight control with a method named DoSomething() decorated with the <ScriptableMember()>

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.