__new__
__new__ is a Python special method that creates and returns an instance before its initialization by __init__ occurs.
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.
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.
Related concepts
- objectproduces · A computational object is a runtime entity or structured value representing data, state, or a resource, with properties or operations defined by its programming model.
- initializerused by · An initializer is a function, declaration, or language construct that establishes the initial value or state of an object, value, computation, or process.
- constructor methodcontrasts with · A constructor is a type-associated operation or callable that creates or initializes an instance and establishes its initial state.
- Singleton patternused by · The Singleton pattern restricts a type or component to one shared instance within a defined scope and provides a way to access it.
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.


