如何使用Python获取当前文件,当前类和当前方法?

时间:2022-11-28 09:03:34
  • 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
  • 代码运行的方法的名称(类的属性)

4 个解决方案

#1


30  

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

#2


9  

import sys

class A:
    def __init__(self):
        print __file__
        print self.__class__.__name__
        print sys._getframe().f_code.co_name

a = A()

#3


5  

self.__class__.__name__  # name of class i'm in

for the rest the sys and trace modules

其余的sys和跟踪模块

http://docs.python.org/library/sys.html http://docs.python.org/library/trace.html

Some more info: https://mail.python.org/pipermail/python-list/2001-August/096499.html and http://www.dalkescientific.com/writings/diary/archive/2005/04/20/tracing_python_code.html

更多信息:https://mail.python.org/pipermail/python-list/2001-August/096499.html和http://www.dalkescientific.com/writingings/diary/archive/2005/04/20/ tracing_python_code.html

did you want it for error reporting because the traceback module can handle that:

你想要它用于错误报告吗,因为回溯模块可以处理:

http://docs.python.org/library/traceback.html

#4


4  

Be very careful. Consider:

要特别小心。考虑:

class A:
    pass

B = A
b = B()

What is the 'class name' of b here? Is it A, or B? Why?

这里b的'类名'是什么?是A还是B?为什么?

The point is, you shouldn't need to know or care. An object is what it is: its name is very rarely useful.

关键是,你不应该知道或关心。对象就是这样的:它的名字很少有用。

#1


30  

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

#2


9  

import sys

class A:
    def __init__(self):
        print __file__
        print self.__class__.__name__
        print sys._getframe().f_code.co_name

a = A()

#3


5  

self.__class__.__name__  # name of class i'm in

for the rest the sys and trace modules

其余的sys和跟踪模块

http://docs.python.org/library/sys.html http://docs.python.org/library/trace.html

Some more info: https://mail.python.org/pipermail/python-list/2001-August/096499.html and http://www.dalkescientific.com/writings/diary/archive/2005/04/20/tracing_python_code.html

更多信息:https://mail.python.org/pipermail/python-list/2001-August/096499.html和http://www.dalkescientific.com/writingings/diary/archive/2005/04/20/ tracing_python_code.html

did you want it for error reporting because the traceback module can handle that:

你想要它用于错误报告吗,因为回溯模块可以处理:

http://docs.python.org/library/traceback.html

#4


4  

Be very careful. Consider:

要特别小心。考虑:

class A:
    pass

B = A
b = B()

What is the 'class name' of b here? Is it A, or B? Why?

这里b的'类名'是什么?是A还是B?为什么?

The point is, you shouldn't need to know or care. An object is what it is: its name is very rarely useful.

关键是,你不应该知道或关心。对象就是这样的:它的名字很少有用。