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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:38:01+00:00 2026-05-13T20:38:01+00:00

I have something roughly like the following. Basically I need to access the class

  • 0

I have something roughly like the following. Basically I need to access the class of an instance method from a decorator used upon the instance method in its definition.

def decorator(view):
    # do something that requires view's class
    print view.im_class
    return view

class ModelA(object):
    @decorator
    def a_method(self):
        # do some stuff
        pass

The code as-is gives:

AttributeError: ‘function’ object has no attribute ‘im_class’

I found similar question/answers – Python decorator makes function forget that it belongs to a class and Get class in Python decorator – but these rely upon a workaround that grabs the instance at run-time by snatching the first parameter. In my case, I will be calling the method based upon the information gleaned from its class, so I can’t wait for a call to come in.

  • 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-13T20:38:02+00:00Added an answer on May 13, 2026 at 8:38 pm

    If you are using Python 2.6 or later you could use a class decorator, perhaps something like this (warning: untested code).

    def class_decorator(cls):
       for name, method in cls.__dict__.iteritems():
            if hasattr(method, "use_class"):
                # do something with the method and class
                print name, cls
       return cls
    
    def method_decorator(view):
        # mark the method as something that requires view's class
        view.use_class = True
        return view
    
    @class_decorator
    class ModelA(object):
        @method_decorator
        def a_method(self):
            # do some stuff
            pass
    

    The method decorator marks the method as one that is of interest by adding a “use_class” attribute – functions and methods are also objects, so you can attach additional metadata to them.

    After the class has been created the class decorator then goes through all the methods and does whatever is needed on the methods that have been marked.

    If you want all the methods to be affected then you could leave out the method decorator and just use the class decorator.

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

Sidebar

Related Questions

Let's say I have objects which look very roughly like this: class object {
I have a function that looks like the following, with a whole lot of
I heave roughly speaking the following hierarchy class Base { public: virtual int value()
I have a html which looks roughly like this: <tr id=row> <td id=cell_1>Cell 1</td>
I Have an MVC Project roughly organized like this: Project 1: MVC app (ViewModels,
All, I have a situation that looks roughly like this: My HTML page a
I have a table tracking daily scores by users. It looks roughly like this:
I have the following table which contains values read every 15 minutes from several
I have the following situation: Three concrete service classes implement a service interface: one
I've got a table in MySQL that looks roughly like: value | count -------------

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.