2

I've been trying to get a method name within this method. I found similar question on stack about getting name for function but solution there did not help

Example:

class Test():
    def meth(self):
        print(meth.__name__) # doesn't work
        print(neth.func_name()) # doesn't work either
        return 0

These cases worked with function but in method I can't even call meth.something

  • Does this answer your question? [Determine function name from within that function (without using traceback)](https://stackoverflow.com/questions/5067604/determine-function-name-from-within-that-function-without-using-traceback) – Green Cloak Guy Jul 24 '20 at 04:09
  • 1
    `self.meth.__name__` or `Test.meth.__name__`. – AChampion Jul 24 '20 at 04:10

1 Answers1

3

As @AChampion pointed out the solution is

self.meth.__name__

Or can be used

Test.meth.__name__
  • Yes, `meth` exists in the `Test` class scope. From other scopes (insides of methods of the class are other scopes too) you need to use the dot notation: `Class.class_attribute` or `object_instance.class_attribute`. Here `Class` is `Test` and `object_instance` is `self` (automatically given as parameter to methods). – pabouk - Ukraine stay strong Jun 03 '22 at 22:43