Python Module Attributes: name, doc, file, dict

Python module has its attributes that describes it. Attributes perform some tasks or contain some information about the module. Some of the important attributes are explained below:

__name__ Attribute

The __name__ attribute returns the name of the module. By default, the name of the file (excluding the extension .py) is the value of __name__attribute.

Example: __name__ Attribute
>>> import math
>>> math.__name__

In the same way, it gives the name of your custom module.

Example: __name__ Attribute
>>> hello.__name__

However, this can be modified by assigning different strings to this attribute. Change as shown below.

Example: Set __name__
def SayHello(name):
    print ("Hi {}! How are you?".format(name))

And check the __name__ attribute now.

>>> import hello
>>> hello.__name__

The value of the __name__ attribute is __main__ on the Python interactive shell.

>>> __name__

When we run any Python script (i.e. a module), its __name__ attribute is also set to __main__. For example, create the following in IDLE.

print("__name__ = ", __name__)

Run the above in IDLE by pressing F5. You will see the following result.

Output in IDLE:
>>> __name__ =  __main__

However, when this module is imported, its __name__ is set to its filename. Now, import the welcome module in the new file with the following content.

import welcome
print("__name__ = ", __name__)

Now run the in IDLE by pressing F5. The __name__ attribute is now "welcome".

__name__ =  welcome

This attribute allows a Python script to be used as an executable or as a module.

Visit __main__ in Python for more information.

__doc__ Attribute

The __doc__ attribute denotes the documentation string (docstring) line written in a module code.

>>> import math  
>>> math.__doc__
'This module is always available. It provides access to the mathematical functions defined by the C standard.'

Consider the the following script is saved as module.
"""This is docstring of test module"""
def SayHello(name):
    print ("Hi {}! How are you?".format(name))

The __doc__ attribute will return a string defined at the beginning of the module code.

>>> import test
>>> test.__doc__
'This is docstring of test module'

__file__ Attribute

__file__ is an optional attribute which holds the name and path of the module file from which it is loaded.

Example: __file__ Attribute
>>> import io
>>> io.__file__

__dict__ Attribute

The __dict__ attribute will return a dictionary object of module attributes, functions and other definitions and their respective values.

Example: __dict__ Attribute
>>> import math
>>> math.__dict__
{'__name__': 'math', '__doc__': 'This module is always available.  It provides a
ccess to the\nmathematical functions defined by the C standard.', '__package__':
 '', '__loader__': <class '_frozen_importlib.BuiltinImporter'>, '__spec__': Modu
leSpec(name='math', loader=<class '_frozen_importlib.BuiltinImporter'>, origin='
built-in'), 'acos': <built-in function acos>, 'acosh': <built-in function acosh>
, 'asin': <built-in function asin>, 'asinh': <built-in function asinh>, 'atan':
<built-in function atan>, 'atan2': <built-in function atan2>, 'atanh': <built-in
 function atanh>, 'ceil': <built-in function ceil>, 'copysign': <built-in functi
on copysign>, 'cos': <built-in function cos>, 'cosh': <built-in function cosh>,
'degrees': <built-in function degrees>, 'erf': <built-in function erf>, 'erfc':
<built-in function erfc>, 'exp': <built-in function exp>, 'expm1': <built-in fun
ction expm1>, 'fabs': <built-in function fabs>, 'factorial': <built-in function
factorial>, 'floor': <built-in function floor>, 'fmod': <built-in function fmod>
, 'frexp': <built-in function frexp>, 'fsum': <built-in function fsum>, 'gamma':
 <built-in function gamma>, 'gcd': <built-in function gcd>, 'hypot': <built-in f
unction hypot>, 'isclose': <built-in function isclose>, 'isfinite': <built-in fu
nction isfinite>, 'isinf': <built-in function isinf>, 'isnan': <built-in functio
n isnan>, 'ldexp': <built-in function ldexp>, 'lgamma': <built-in function lgamm
a>, 'log': <built-in function log>, 'log1p': <built-in function log1p>, 'log10':
 <built-in function log10>, 'log2': <built-in function log2>, 'modf': <built-in
function modf>, 'pow': <built-in function pow>, 'radians': <built-in function ra
dians>, 'remainder': <built-in function remainder>, 'sin': <built-in function si
n>, 'sinh': <built-in function sinh>, 'sqrt': <built-in function sqrt>, 'tan': <
built-in function tan>, 'tanh': <built-in function tanh>, 'trunc': <built-in fun
ction trunc>, 'pi': 3.141592653589793, 'e': 2.718281828459045, 'tau': 6.28318530
7179586, 'inf': inf, 'nan': nan}

dir() is a built-in function that also returns the list of all attributes and functions in a module.

Know Module Attributes and Methods
Know Module Attributes and Methods

Learn more about module attributes in Python Docs.