__name__ attribute
In Python, the __name__ attribute identifies the defining name or execution context of an object such as a module, class, or function.
The Quick Python Book, Fourth Edition
The __name__ attribute of a class contains the class's defining name.
- How it is used
- Uses class names to inspect the inheritance structure associated with an instance.
- How deep it goes
- Demonstrates attribute access alongside class and base-class inspection, without exploring implementation internals.
- Examples
- Inspects an instance of class B, retrieving its name and base class A.
- What you can do after
- Evaluate and understand an instance’s class name and base classes using special attributes.
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 →
Also covered in
- Python Workout, Second Edition
- Python How-To
- Build Your Own Robot: Using Python, CRICKIT, and Raspberry Pi
- Tiny Python Projects: Learn coding and testing with puzzles and games
What to know first
- functionA function is a callable unit of program code that performs a computation or operation, can accept inputs, and may produce a result or side effect.
- objectAn object is a distinct computational entity or value represented with associated data, structure, identity, or behavior.
- PythonPython is a high-level, general-purpose, dynamically typed programming language with readable syntax and support for multiple programming paradigms.
- MapA map is an associative data structure that stores key-value associations and supports retrieving values by key.
Related concepts
- entry pointconfigures · An entry point is a designated starting location or interface through which a program, component, process, or interaction begins.
- functionpart of · A function is a callable unit of program code that performs a computation or operation, can accept inputs, and may produce a result or side effect.
- classused by · A class is a programming-language construct that defines a type’s structure and behavior and provides a basis for creating instances.
Questions about __name__ attribute
What is __name__ attribute?
In Python, the __name__ attribute identifies the defining name or execution context of an object such as a module, class, or function.
Which Manning books explain __name__ attribute?
The Quick Python Book, Fourth Edition and Python Workout: 50 ten-minute exercises.
What should I know before learning __name__ attribute?
Manning books that teach __name__ attribute usually introduce function, object, Python and Map first.
What is __name__ attribute part of?
__name__ attribute is part of function. A function is a callable unit of program code that performs a computation or operation, can accept inputs, and may produce a result or side effect.
What is __name__ attribute also called?
__name__ attribute is also called __name__ variable.
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.

