python模块的name属性
python模块的name属性
一个模块被另一个程序第一次引入时,其主程序将运行。
1、如果想在模块被引入时,模块中的某一程序块不执行,可以用name属性来使该程序块仅在该模块自身运行时执行。
2、每个模块都有一个name属性,当其值是'main'时,表明该模块自身在运行,否则是被引入。
说明:*name*与*main*底下是双下划线,__是这样去掉中间的那个空格。
实例
#!/usr/bin/envpython
#-*-coding:utf-8-*-
#author:xulinjietime:2017/10/13
defmain():
if__name__=='__main__':
print('程序自身在运行')
else:
print('我来自另一模块')
main()
print(__name__)
以上就是python模块的name属性,希望对大家有所帮助。更多Python学习教程请关注IT培训机构:千锋教育。

相关推荐HOT
更多>>
python函数装饰器的应用过程
python函数装饰器的应用过程1、应用过程一般是定义在一个独立模块中,使用时会被导入到其它模块;函数装饰器会在内部定义一个新函数,然后返回。...详情>>
2023-11-10 22:33:56
python实现异步的两种框架
python实现异步的两种框架Python实现异步的框架有很多,但是核心思想大概是基于下面两种方式twistergeventtwister1、twistertwister思想是将异...详情>>
2023-11-10 22:11:26
python函数形式如何实现生成器
python函数形式如何实现生成器1、说明生成器也是迭代器,但只能迭代一次。这是因为它们没有存储所有的值,而是在运行过程中产生值。你一直在使...详情>>
2023-11-10 21:50:58
python描述符的两种类型
python描述符的两种类型1、分类数据描述符:实现了__get__和__set__两种方法的描述符非数据描述符:只实现了__get__一种方法的描述符2、实例#数...详情>>
2023-11-10 21:30:01