Ockonal Ockonal - 7 months ago 27
Python Question

How do I get the current file, current class, and current method with Python?


  • Name of the file from where code is running

  • Name of the class from where code is running

  • Name of the method (attribute of the class) where code is running


Answer

Here is an example of each:

from inspect import stack

class Foo:
    def __init__(self):
    	print __file__
    	print self.__class__.__name__
    	print stack()[0][3]

f = Foo()