Master Chef India Season 2 | LOVERS MEDLEY 3 | Malcolm Sinclair

, asked
Views : 3

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

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

1 Answer
Rajendra Dharmkar

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.

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__

This gives the output

foo
Bar

Advertisements
Related Questions