__new__ — Easy Examples
Creates and returns a new instance before __init__ is called
Implementing __new__
Basic implementation of __new__ in a class.
python
class Example: def __new__(self): return "Example __new__" obj = Example() print(obj)
__new__ creates and returns a new instance before __init__ is called. Implementing it lets you customize how Python interacts with your objects.
__new__ in action
Seeing __new__ called by Python's built-in operations.
python
# How Python calls __new__ automatically class Demo: def __init__(self, value): self.value = value def __new__(self): print(f"__new__ was called!") return self d = Demo(42) # This triggers __new__: print(d)
Python automatically calls __new__ when you use the corresponding operator or function on your object.
Want to try these examples interactively?
Open Easy Playground