cerhovice
cerhovice

Reputation: 686

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?

Upvotes: 3

Views: 335

Answers (1)

Kasravnd
Kasravnd

Reputation: 107297

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

Upvotes: 1

Related Questions