为什么代码打印“测试消息”尽管我只是写了打印属性的代码。

发布于 2025-01-21 09:03:18 字数 438 浏览 1 评论 0原文

我不知道为什么它同时打印“测试消息”和“ 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

朦胧时间 2025-01-28 09:03:18

导入第二个将在second.py中运行所有代码。
print(“测试消息”)也执行。
如果要防止此使用以下

a = 'ABC'
if __name__ == "__main__":
    print("test message")

如果__name__ ==“ __ main __”:才能在从该文件运行时运行。

import second will run all code in second.py.
print("test message") is also executed.
If you want to prevent this use below

a = 'ABC'
if __name__ == "__main__":
    print("test message")

if __name__ == "__main__": will only run when run from that file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文