What does the (__ name__) module do?

I am puzzled by this line of code:

email = Module(__name__)

      

I would guess it is just creating an alias to reference the module I'm working in? If not, what does it do?

+3


source to share


1 answer


In module, the module name (as a string) is available as the value of a global variable __name__

.

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

      

But email = Module(__name__)

will raise the value NameError

: (name 'Module' is undefined). and if you have defined a name Module

for example use itertools(__name__)

as it cannot be called it will raise TypeError

.



Since it __name__

is a module attribute, you cannot skip it.

Also you can find __name__

in the result the result of the function dir()

, which is used to determine the names that the 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']

      

+1


source







All Articles