Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get class name from bound method using inspect? [duplicate]

class MyClass:
  def my_method(self):
    print(get_context())

MyClass().my_method()

I need get next line:

MyClass::my_method

sys._getframe(2).f_code.co_name gives me only "my_method". How to get also class name?

like image 941
Artem Selivanov Avatar asked May 13 '26 20:05

Artem Selivanov


1 Answers

You can get your classname by calling __class__.__name__ from self.

class Foo(object):
    def bar(self):
        print(self.__class__.__name__)

Foo().bar()

Output: Foo

like image 52
idjaw Avatar answered May 16 '26 08:05

idjaw



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!