liveBook · Manning

__new__

__new__ is a Python special method that creates and returns an instance before its initialization by __init__ occurs.

How 3 Manning books explain it

Python How-To

__new__ is Python's special class-level method that creates and returns a new instance before initialization.

How it is used
Explains object construction and customizes instance creation when ordinary allocation needs to change.
How deep it goes
Explains the sequential call order, returned identity, manual simulation, and subclass customization, while recommending ordinary use of __init__.
Examples
Uses Task identity tracing and PokerOrder normalization to demonstrate creation order and custom sorting.
What you can do after
Understand instance creation, trace object identity, and customize construction for subclasses such as PokerOrder.

Read how Python How-To explains __new__ →

Python Workout: 50 ten-minute exercises

A Python special method responsible for creating and returning a new instance before initialization occurs.

How it is used
Clarifies what happens when a class is called and distinguishes object creation from instance initialization.
How deep it goes
Explains the creation-to-initialization sequence but stops short of implementing custom __new__.
Examples
Uses Foo(10), self.x, and diagrams tracing creation, initialization, attribute assignment, and return.
What you can do after
Evaluate ordinary construction correctly and favor __init__ over redefining __new__ in typical classes.

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

Python Workout, Second Edition

__new__ is a Python special method that creates and returns a new instance before initialization occurs.

How it is used
Explains why __init__ is initialization rather than the method that creates an instance.
How deep it goes
Traces creation, initialization, and return, while deferring custom __new__ implementation as rarely necessary.
Examples
Uses Foo(10), self.x, and f.x to demonstrate creation followed by attribute initialization.
What you can do after
Understand the execution order behind class calls and avoid mistaking __init__ for the instance-creation method.

Read how Python Workout, Second Edition explains __new__ →

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.