简体   繁体   中英

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__ .

>>> import itertools as it
>>> it.__name__
'itertools'

But email = Module(__name__) will raise a NameError : (name 'Module' is not defined). and if you defined the name Module for example use itertools(__name__) as it is not callable it raise a TypeError .

So as __name__ is a module attribute you cant pass it alone .

Also you can find __name__ in the dir() function result that is used to find out which names a module defines.

>>> 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']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM