liveBook · Manning

__name__ attribute

Also called __name__ variable

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
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

What to know first

Related concepts

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.