老妪能懂的 Python中的if
发布时间:2024-11-15 18:09
先来创建一个hello.py的文件
def main():
print("hello world")
main()
输出结果:
hello world
再创建一个 world.py的文件,把hello导进去
from hello import main
main()
world.py执行结果:
hello world
hello world
没错,两个hello world。因为hello里边打印了一遍hello world,调用到world后又打印了一遍hello world。
把hello加上main
def main():
print("hello world")
if __name__ == "__main__":
main()
运行结果:
hello world
给hello加上if __name__ == '__main__',在执行world.py
from hello import main
main()
执行结果:
hello world
变成只打印一个hello world了。
if __name__ == '__main__'的意思是:当.py文件被直接运行时, if __name__ == '__main__'之下的代码块将被运行;当.py文件以模块形式被导入时, if __name__ == '__main__'之下的代码块不被运行。
网址:老妪能懂的 Python中的if https://www.yuejiaxmz.com/news/view/84563
下一篇:水池修缮的实施方案(附施工方案)
相关内容
Python中if如何简单地理解Python中的if
python if是如何判断true或False的呢? is和== 有什么区别呢?
python中的print()语句中的end=''是什么意思
Python中的遇到的错误(持续更新)
python中pow
Python :=海象运算符最简单的解释
基于python的膳食健康推荐系统
【计算机视觉】基于Python—OpenCV的手势识别详解(一)
Python实现经典还钱问题算法:优化财务管理的编程技巧