Computer >> Computer tutorials >  >> Programming >> Python

What does built-in class attribute __name__ do in Python?


This built-in attributes prints the name of the class, type, function, method, descriptor, or generator instance.

For example, if the python interpreter is running that module (the source file) as the main program, it sets the special __name__ variable to have a value "__main__". If this file is being imported from another module, __name__ will be set to the module's name.

Example

The following code illustrates the use of __name__.

class Bar(object):
    def foo():

       """ This is an example of how a doc_string looks like.

          This string gives useful information about the function being defined.

            """

    pass

    print foo.__name__
print Bar.__name__

Output

This gives the output

foo
Bar