入口文件和普通模块内置变量的区别

__package__如果是NonType的时候,会报错

Traceback (most recent call last):
  File "/Users/scottxiong/Desktop/end/python/p/p4.py", line 10, in <module>
    print("package:"+__package__ or "当前模块不属于任何包" )
TypeError: can only concatenate str (not "NoneType") to str
'''
    this is a doc
    hello world
'''
print("name:"+__name__)
print("package:"+(__package__ or "当前模块不属于任何包" ))
print("file:"+__file__)
print("doc:"+(__doc__ or "当前模块没有注释"))
name:__main__
package:当前模块不属于任何包  
file:/Users/scottxiong/Desktop/end/python/p/p4.py
doc:
    this is a doc
    hello world

入口文件和内置模块在内置变量上的区别:

入口文件(即可执行文件的)的name是__main__,模块的name是去掉.py后的文件名

入口文件是没有package,或者说package为NoneType,

results matching ""

    No results matching ""