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

  • Home
  • SEARCH
  • 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 6916223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:34:27+00:00 2026-05-27T09:34:27+00:00

Consider the following code: class MyCustomDescriptor: def __init__(self,foo): self._foo = foo def __call__(self,decorated_method): #

  • 0

Consider the following code:

class MyCustomDescriptor:
    def __init__(self,foo):
        self._foo = foo

    def __call__(self,decorated_method):
        # Here's my question...  Is there any way to get a reference to the
        # type (ClassA or ClassB) here?
        return self

    def __get__(self,instance,type):
        # Clearly at this point I can get the type of the class.
        # But it's too late, I would have liked
        # to get it back in __call__.
        return 10

class ClassA:
    @MyCustomDescriptor(foo=1)
    def some_value(self): pass

class ClassB:
    @MyCustomDescriptor(foo=1)
    def some_value(self): pass

The reason I’d like to get a reference to the class is that I’d like to add some static data to the class with the decorated function/method. I realize this is somewhat atypical but for what I’m doing, it would be helpful.

ANSWER – Can’t be done. Based on one of the responses below I inspected the stack from within call and was able to get the fully qualified class where the descriptor is being used (ClassA or ClassB in my example). But you can’t turn this into a type/class because the type/class is still being parsed (or whatever the right term is in python). In other words, python comes across ClassA and starts parsing it. While parsing it, it comes across the descriptor and invokes init and call on the descriptor. ClassA still hasn’t finished getting parsed. Therefore regardless of the fact that you can get a fully qualified module/class name from within call, you can’t turn it into a type.

  • 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-27T09:34:28+00:00Added an answer on May 27, 2026 at 9:34 am

    At the point where the decorator is being applied, some_value is just a function, not a method. So, no, there is no way for the function to know it is associated with a particular class.

    Two alternatives are:

    • Pass the class name to MyCustomDescriptor (along with foo), or
    • Use a class decorator to create the descriptor some_value.

    The class decorator might look something like this:

    def register(method_name,foo):
        def class_decorator(cls):
            method=getattr(cls,method_name)
            class MyCustomDescriptor(object):
                def __get__(self,instance,type):
                    result=method(instance)
                    return '{c}: {r}'.format(c=cls.__name__,r=result)
            setattr(cls,method_name,MyCustomDescriptor())
            return cls
        return class_decorator
    
    @register('some_value',foo=1)
    class ClassA:
        def some_value(self):
            return 10
    

    For example, running

    a=ClassA()
    print(a.some_value)
    

    yields

    ClassA: 10
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

consider the following python code class Base(object): def __init__(self): self.foo = 5 class Derived(Base):
consider the following code: class MyClass(object): def __init__(self): self.data_a = np.array(range(100)) self.data_b = np.array(range(100,200))
Consider the following example code class A: def __init__(self, i): self.i = i print("Initializing
Consider the following code: class Foo(var name: String = bar) Now i try to
Consider the following code: class Foo { // boring parts omitted private TcpClient socket;
Consider the following code: class Program { void Foo<T>() { } static void Main(string[]
Consider the following code: public class Foo { private static final Object LOCK =
Consider the following example code: class Foo { }; class Bar : public Foo
Consider the following code: Class Demo { Person person = new Person(foo); // assume
Consider the following code: abstract class Foo<T> where T : Foo<T>, new() { void

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.