我有一個基類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