1

Here __class__ should not be confused with self.__class__ which I am already able to access with the inspect module:

import inspect


class A:

    def __init__(self):
        print(__class__.__name__)  # I want to move this statement inside f
        f()


class B(A):
    pass


def f():
    prev_frame = inspect.currentframe().f_back
    self = prev_frame.f_locals["self"]
    print(self.__class__.__name__)


B()  # prints A B
Géry Ogam
  • 6,336
  • 4
  • 38
  • 67

1 Answers1

3

The implicit __class__ reference is created at compile-time only if you actually reference it within the method (or use super). For example this code:

class Foo:
    def bar(self):
        print('bar', locals())

    def baz(self):
        print('baz', locals())

        if False:
            __class__

if __name__ == '__main__':
    foo = Foo()

    foo.bar()
    foo.baz()

Produces this output:

bar {'self': <__main__.Foo object at 0x10f45f978>}
baz {'self': <__main__.Foo object at 0x10f45f978>, '__class__': <class '__main__.Foo'>}

To find the calling function's class (in most cases) you could chain together a few CPython-specific inspect incantations:

  1. Find the calling function: How to get current function into a variable?
  2. Find that function's class: Get defining class of unbound method object in Python 3

I wouldn't recommend it.

Blender
  • 289,723
  • 53
  • 439
  • 496
  • Since the OP only needs the name of the class, they could just use `inspect` to get the calling function and extract the class name from that function's `__qualname__`. No real need to get the class object. – Aran-Fey Jun 02 '19 at 18:31
  • @Aran-Fey: The question title referred to using the `__class__` reference so I assumed `__class__.__name__` is just an example. But yes, if all you need is the name of the parent class then you could just use the approach from the first linked question and `__qualname__`. – Blender Jun 02 '19 at 18:33
  • I was actually trying to reimplement the `super` class in Python to understand its behavior, so I need the class object @Aran-Fey. And I was at the syntactic sugar part `super() == super(__class__, self)`. Why did they choose to create the `__class__` implicit closure reference only "if any methods in a class body refer to either `__class__` or `super`" instead of always? My `super` class reimplementation is not called `super` so `__class__` is not created and I am left with @Blender CPython hack (thank you by the way). – Géry Ogam Jun 03 '19 at 08:14
  • @Maggyero: I'm not very familiar with CPython internals or its history so I'm not 100% sure. It's been this way since the first commit that introduced it. – Blender Jun 03 '19 at 18:09