liveBook · Manning

__name__ attribute

In Python, the __name__ attribute identifies the defining name or execution context of an object such as a module, class, or function.

How 2 Manning books explain it

The Quick Python Book, Fourth Edition

The __name__ attribute of a class contains the class's defining name.

How it is used
Identifies a class by name while inspecting an instance’s class and inheritance structure.
How deep it goes
Demonstrates attribute access and related class inspection, but does not explore attribute implementation.
Examples
Shows an instance of subclass `B` yielding `'B'` through its class’s `__name__` attribute.
What you can do after
Evaluate/understand a class name retrieved from an instance, while distinguishing it from a module’s `__name__` variable.

Read how The Quick Python Book, Fourth Edition explains __name__ attribute →

Python Workout: 50 ten-minute exercises

A Python attribute containing the name associated with a module or class.

How it is used
Distinguishes directly executed modules from imported modules and retrieves a class name from an object.
How deep it goes
Explains module execution, namespace assignment, and dynamic class-name lookup, stopping short of broader attribute internals.
Examples
Uses the __name__ guard and self.__class__.__name__ to demonstrate module control flow and class identification.
What you can do after
Apply it via a direct-execution guard or dynamic class-name lookup.

Read how Python Workout: 50 ten-minute exercises explains __name__ attribute →

Related concepts

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.