__name__
是python中的特殊属性,并具有模块名称的值。如何使用__name__属性来获得相应的输出?
try2.py,try1.py,try3.py的__name__
的值分别是'try2','try1'和'try3'。
如果在try2.py中分别获取文件try1.py和try3.py中的相应输出,我应该替换哪些内容?
文件:try2.py
class A(object):
a=[]
def add(self,p,q):
return p+q
def minus(self,p,q):
return p-q
#I know that if i replace 'try1' by 'try2' in if condition,
#then this can be executed where try2.py will be imported
if(__name__=='try1'):
print('runs only if module name is try1')
elif(__name__=='try3'):
print('runs only if module name is try3')
文件:try1.py
import try2
print(try2.A().add(2,3))
文件:try3.py
import try2
print(try2.A().minus(2,3))
...什么?如果导入它,try2.py中的'__name__'将会是“try2”。 –
是的,我想分别在try1.py和try3.py中打印不同的行 –
@VibhorVerma,您的评论无助于清除任何内容。另外try3在哪里? –