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

Max Frai picture Max Frai · May 21, 2009 · Viewed 16.6k times · Source
  • 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

Andrew Hare picture Andrew Hare · May 21, 2009

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()