[英]What does Module(__name__) do?
I am puzzled by this line of code: 我对这行代码感到困惑:
email = Module(__name__)
I would guess this is simply creating an alias to refer to the module that I am in? 我猜这只是创建一个别名来引用我所在的模块? If not, then what does this do?
如果没有,那么这有什么用呢?
Within a module , the module's name (as a string) is available as the value of the global variable
__name__
.在模块中 ,模块的名称(作为字符串)可用作全局变量
__name__
的值。
>>> import itertools as it
>>> it.__name__
'itertools'
But email = Module(__name__)
will raise a NameError
: (name 'Module' is not defined). 但是
email = Module(__name__)
会引发NameError
:(名称'Module'未定义)。 and if you defined the name Module
for example use itertools(__name__)
as it is not callable it raise a TypeError
. 如果您定义名称
Module
例如使用itertools(__name__)
因为它不可调用它会引发TypeError
。
So as __name__
is a module attribute you cant pass it alone . 因此
__name__
是一个模块属性,你不能单独传递它。
Also you can find __name__
in the dir()
function result that is used to find out which names a module defines. 您还可以在
dir()
函数结果中找到__name__
,该结果用于查找模块定义的名称。
>>> dir(itertools)
['__doc__', '__name__', '__package__', 'chain', 'combinations', 'combinations_with_replacement', 'compress', 'count', 'cycle', 'dropwhile', 'groupby', 'ifilter', 'ifilterfalse', 'imap', 'islice', 'izip', 'izip_longest', 'permutations', 'product', 'repeat', 'starmap', 'takewhile', 'tee']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.