Python object creation
last modified November 30, 2026 Python 3.15
In this article we look at how Python creates objects and where
__new__ fits into the process.
Calling a class, as in Point(1, 2), runs two special
methods. __new__ creates the object and
__init__ fills it with data. Most classes define only
__init__ and inherit __new__ from
object. This article follows the two steps one by one and
shows the situations where the difference between them matters.
The best-known uses of __new__, such as singletons,
immutable subclasses, and metaclasses, are covered in
Python __new__ method; the
__init__ method is described in
Python __init__ method.
Two steps of object creation
The two steps of a class call can be performed separately. This shows what each step does.
class Point:
def __init__(self, x, y):
print(f"initializing with {x}, {y}")
self.x = x
self.y = y
p = Point(1, 2)
print(p.x, p.y)
q = Point.__new__(Point)
print(type(q).__name__, vars(q))
Point.__init__(q, 3, 4)
print(q.x, q.y)
Point(1, 2) creates an object and initializes it in one
call. The second part does the same by hand.
Point.__new__(Point) returns a new Point
object with an empty attribute dictionary: the object exists, but
__init__ has not run. Calling
Point.__init__(q, 3, 4) then sets the attributes. Note that
__new__ takes the class as its first argument, while
__init__ takes the object.
$ python calling_class.py
initializing with 1, 2
1 2
Point {}
initializing with 3, 4
3 4
Arguments of __new__ and __init__
Both methods receive the arguments of the class call. When a class
defines __new__, it decides what to pass on to
object.__new__.
class Point:
def __new__(cls, x, y):
print(f"__new__ gets {cls.__name__}, {x}, {y}")
return super().__new__(cls)
def __init__(self, x, y):
print(f"__init__ gets {x}, {y}")
self.x = x
self.y = y
p = Point(1, 2)
class Empty:
pass
class Forwarding:
def __new__(cls, x):
return super().__new__(cls, x)
def __init__(self, x):
self.x = x
for cls in [Empty, Forwarding]:
try:
cls(5)
except TypeError as e:
print(f"{cls.__name__}: {e}")
Point(1, 2) calls __new__(Point, 1, 2) and
then __init__(p, 1, 2). object.__new__ needs
only the class, so Point passes just cls to
it. Forwarding passes the extra argument on and gets a
TypeError. The Empty class defines neither
method, so it accepts no arguments at all.
$ python arguments.py __new__ gets Point, 1, 2 __init__ gets 1, 2 Empty: Empty() takes no arguments Forwarding: object.__new__() takes exactly one argument (the type to instantiate)
Creating objects without __init__
Calling cls.__new__(cls) creates an object that skips
__init__. This is useful in an alternative constructor
that builds an object from data that has already been checked.
class User:
def __init__(self, name, email):
print(f"checking {email}")
if "@" not in email:
raise ValueError(f"invalid email: {email}")
self.name = name
self.email = email.lower()
@classmethod
def from_row(cls, row):
user = cls.__new__(cls)
user.name, user.email = row
return user
u1 = User("Lucia", "Lucia@Example.com")
print(u1.name, u1.email)
rows = [("Peter", "peter@example.com"), ("Anna", "anna@example.com")]
users = [User.from_row(row) for row in rows]
for user in users:
print(type(user).__name__, user.name, user.email)
The __init__ method validates and normalizes the email
address. The from_row class method creates users from rows
read back from storage, where the data was checked when it was saved.
It creates an empty object with cls.__new__(cls) and sets
the attributes directly, so the check is not repeated. Because it uses
cls, a subclass of User would get objects of
its own type. The alternative constructor must set every attribute
that __init__ would set.
$ python skip_init.py checking Lucia@Example.com Lucia lucia@example.com User Peter peter@example.com User Anna anna@example.com
Choosing the class in __new__
__new__ may return an object of a subclass. Python then
still calls __init__, because the object is an instance of
the class that was called.
import math
class Shape:
def __new__(cls, kind, size):
if cls is Shape:
kinds = {"square": Square, "circle": Circle}
if kind not in kinds:
raise ValueError(f"unknown shape: {kind}")
cls = kinds[kind]
return super().__new__(cls)
def __init__(self, kind, size):
print(f"{type(self).__name__}.__init__ runs")
self.size = size
class Square(Shape):
def area(self):
return self.size ** 2
class Circle(Shape):
def area(self):
return round(math.pi * self.size ** 2, 2)
shapes = [Shape("square", 3), Shape("circle", 2), Square("square", 5)]
for s in shapes:
print(type(s).__name__, s.area(), isinstance(s, Shape))
try:
Shape("hexagon", 1)
except ValueError as e:
print(e)
Calling Shape("square", 3) enters
Shape.__new__, which looks up the subclass for the given
kind and creates an object of that class. The result is a
Square, which is a Shape, so Python passes it
to __init__ with the original arguments. When a subclass
is called directly, cls is not Shape and no
lookup is done. If __new__ returned an object that is not
an instance of the class, __init__ would be skipped.
$ python pick_subclass.py Square.__init__ runs Circle.__init__ runs Square.__init__ runs Square 9 True Circle 12.57 True Square 25 True unknown shape: hexagon
Reusing instances
__new__ can return an existing object instead of a new
one, for example to share one instance per value. In that case
__init__ runs again on the old object.
class Color:
_cache = {}
def __new__(cls, name):
key = name.lower()
if key not in cls._cache:
print(f"creating {key}")
obj = super().__new__(cls)
obj.name = key
cls._cache[key] = obj
return cls._cache[key]
def __init__(self, name):
self.opacity = 1.0
red = Color("red")
blue = Color("blue")
also_red = Color("RED")
print(red is also_red, red is blue)
print(len(Color._cache))
red.opacity = 0.5
print(red.opacity)
Color("Red")
print(red.opacity)
The colors are cached by their lowercase name, so
Color("RED") returns the same object as
Color("red"), and only two objects are created. The cached
object is complete when it leaves __new__. The
__init__ method, however, runs on every call: creating
Color("Red") again resets the opacity of the shared object
to 1.0. A class that reuses instances should do all its setup in
__new__ and define no __init__.
$ python cache_by_value.py creating red creating blue True False 2 0.5 1.0
How copy creates objects
The copy module does not call the class. It creates the
copy with __new__ and transfers the attributes, so
__init__ does not run.
import copy
class Document:
def __init__(self, title):
print(f"__init__ for {title}")
self.title = title
self.tags = []
doc = Document("notes")
doc.tags.append("draft")
shallow = copy.copy(doc)
deep = copy.deepcopy(doc)
print(shallow.title, shallow.tags, deep.tags)
print(shallow.tags is doc.tags, deep.tags is doc.tags)
The message from __init__ is printed only once, for the
original document. Both copies get their attributes from the original
instead. The shallow copy shares the tags list with the
original, while the deep copy has a copy of the list. The
pickle module creates objects the same way when it loads
them.
$ python copy_skips_init.py __init__ for notes notes ['draft'] ['draft'] True False
Arguments for __new__ when copying
When __new__ requires arguments, the copy machinery must
know what to pass. The __getnewargs__ method returns them.
import copy
class Label(str):
def __new__(cls, text, color):
obj = super().__new__(cls, text.upper())
obj.color = color
return obj
class SafeLabel(Label):
def __getnewargs__(self):
return str(self), self.color
ok = Label("ok", "green")
print(ok, ok.color)
try:
copy.copy(ok)
except TypeError as e:
print(e)
safe = SafeLabel("done", "blue")
dup = copy.copy(safe)
print(dup, dup.color, type(dup).__name__)
Label is a string subclass with an extra required
argument. A string's value cannot be changed after creation, so it is
set in __new__. The inherited __getnewargs__
of str returns only the text, and copying fails because
color is missing. SafeLabel returns both
values, so copy.copy calls
SafeLabel.__new__(SafeLabel, "DONE", "blue"). The same
method also makes such objects work with pickle.
$ python getnewargs.py OK green Label.__new__() missing 1 required positional argument: 'color' DONE blue SafeLabel
Source
Data model: __new__, Data model: __init__, __getnewargs__, copy – Shallow and deep copy operations
In this article we have split object creation into its two steps,
followed the arguments of a class call to __new__ and
__init__, created objects without running
__init__, chosen a subclass in __new__, reused
instances, and seen how copy creates objects with
__new__ and __getnewargs__. See also
Python __new__ method,
Python metaclasses, and
Python callable objects.
Author
List all Python tutorials.