1

So, i have a class like this:

class Test:
    def test69():
        print(classname)

and That function (test69()) prints the Name of the Class that it is in. Like, a Function is in a Class, and the Function prints the Name of the Class that it is in to the Console.

I am working on Python 3.11, if that Helps any.

Does anyone know how to do that?

Thanks!

1 Answers1

1

It is available via the __class__ reference:

class Test:
    def test69():
        print(__class__.__name__)
wim
  • 338,267
  • 99
  • 616
  • 750
  • Is a function declared in this way static? I compared the output with one wrapped with `staticmethod` and seem the same – cards Nov 07 '22 at 23:31
  • 1
    Nope, it is not the same. A static method could be called on an instance like `Test().test69()`, but this function could not be called in that way, only by `Test.test69()`. To see the difference you have to bypass the descriptor protocol with e.g. `object.__getattribute__(Test, "test69")`. – wim Nov 08 '22 at 01:40