我有一个基类Base
是在模块base.py
的模块名称。获取类对象哪个模块__main__
class Base:
def __init__(self):
print(self.__module__)
此外,还有一个子类Child
是在模块child.py
。
from test.base import Base
class Child(Base):
pass
if __name__ == '__main__':
c = Child()
我运行了python child.py
。 我想声明print(self.__module__)
打印child
或child.py
,不__main__
因为它是目前印刷。
P.S.如果没有在子类
这不是我的意思。如果我有很多类的复杂'mro',我需要像我有课程那样编写很多'if isinstance'? – hasam
试试'self .__ class__'? – Jing