首页 分享 (Tensorflow之五)Python 中 if

(Tensorflow之五)Python 中 if

来源:花匠小妙招 时间:2025-06-25 16:20

最新推荐文章于 2021-01-29 03:37:25 发布

abiggg 于 2017-12-30 11:51:20 发布

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

__name__:模块名
python中的模块名可以分成两类,一类是xxx.py文件,那么模块名就是xxx,例如test.py,那么模块名是test,调用方法就是import test;另一类是__main__,当直接运行xxx.py文件是,缺省调用的模块名是__main__;
直接上代码:

test.py !/usr/bin/python def func(): print("test 1") print("test 2") def main(): func() if __name__ == "__main__": print("test 3") main() else: print("test 4")

运行结果:

test 2 test 3 test 1

分析:python是从上到下的运行顺序,所以先执行test 2,因为是直接运行,模块名即为__main__,调用if分支,先后执行test 3 ,test 1;

test2.py import test print("test 5") test.func() if __name__ == "__main__": print("test 6") else: print("test 7")

运行结果:

test 2 test 4 test 5 test 1 test 6

分析:import test时,开始执行test.py,首先输出test2,此时因为是import 调用,模块名为test, 这时,调用了else分支,输出test 4;对于test 2 而言,是直接运行,因此模块名仍为main。

相关知识

tensorflow识别花朵
【实战】tensorflow 花卉识别
Tensorflow:TensorFlow基础(一)
TensorFlow学习记录(八)
深度学习入门——基于TensorFlow的鸢尾花分类实现(TensorFlow
基于TensorFlow训练花朵识别模型的源码和Demo
TensorFlow基础学习笔记
Tensorflow 初级教程(一)
探索Python中的花朵识别算法:从恶之花到智能分类
图像分类之花卉图像分类(五)测试数据

网址: (Tensorflow之五)Python 中 if https://www.huajiangbk.com/newsview2079345.html

所属分类:花卉
上一篇: 一枚芯片同时检测12种常见呼吸道
下一篇: 涨知识!原来病原体的诊断方法有这

推荐分享