为什么代码打印“测试消息”尽管我只是写了打印属性的代码。
我不知道为什么它同时打印“测试消息”和“ ABC”,而不仅仅是“ ABC”。 我认为刚刚写了用于打印属性“ A”的代码,但它会打印更多内容!
我有两个模块:“ first.py”& “ second.py”
first.py是:
import second
print(second.a)
second.py is:
a="ABC"
print("test message")
输出is:
test message
ABC
I don't know why it prints both "test message" and "ABC" instead of just "ABC".
I think just wrote the code for printing the attribute "a" but it prints something more!
I have two modules: "first.py" & "second.py"
first.py is:
import second
print(second.a)
second.py is:
a="ABC"
print("test message")
OUTPUT is:
test message
ABC
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
导入第二个
将在second.py
中运行所有代码。print(“测试消息”)
也执行。如果要防止此使用以下
如果__name__ ==“ __ main __”:
才能在从该文件运行时运行。import second
will run all code insecond.py
.print("test message")
is also executed.If you want to prevent this use below
if __name__ == "__main__":
will only run when run from that file.