Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

How the Python Default Constructor Works

Understand the python default constructor, what arguments it accepts, and when to define your own __init__ for reliable state initialization.

~4 min readPython
#Python classes#__init__ method#object initialization#Python OOP
Illustration of a Python class with a default constructor creating an empty instance with no attributes.

When you define a class in Python without an __init__ method, the class still has a constructor. Calling ClassName() creates an instance, and the behavior comes from the object base class. This implicit behavior is the Python default constructor, and understanding it matters because it determines what arguments your class accepts, what state a new instance has, and when you must override it.

What the Default Constructor Does

In Python 3, every class has object in its method resolution order. When you do not define __init__, the class uses object.__init__, which accepts no arguments besides the instance itself. If you do not define __new__ either, object.__new__ allocates the instance.

python
class Point: pass p = Point() print(p) # <__main__.Point object at 0x...>

The instance is created successfully, but it has no attributes. Accessing an attribute before assigning it raises AttributeError:

python
p.x = 3 # works, sets an attribute print(p.x) # 3

The default constructor does not initialize any state. The instance is an empty container until you assign attributes.

What Arguments the Default Constructor Accepts

Because object.__new__ and object.__init__ both reject extra arguments, a plain class without either method overridden accepts only zero arguments. Passing arguments raises a TypeError before initialization starts:

python
class Point: pass Point(1, 2) # raises TypeError

For a plain class, the error comes from object.__new__, not object.__init__. object.__new__ allocates the instance first, and it rejects the extra arguments before __init__ has a chance to run. This is a common source of confusion for developers coming from languages where a default constructor accepts nothing but the language silently ignores extra arguments. Python does not ignore them; it raises an error.

The Role of __new__ in the Default Constructor

The term "constructor" in Python technically covers two methods: __new__ and __init__. __new__ is responsible for creating the instance, and __init__ for initializing it. The default __new__ from object creates an instance of the calling class. The default __init__ does nothing.

python
class Point: def __new__(cls): print("creating instance") return super().__new__(cls) def __init__(self): print("initializing instance") p = Point()

This prints:

code
creating instance initializing instance

Most classes only need to override __init__. Overriding __new__ is most common for cases like immutable types or singleton patterns. When you rely on the default constructor, both methods come from object, and the sequence is: object.__new__ allocates, then object.__init__ does nothing.

When You Must Override the Default Constructor

The default constructor is sufficient when a class needs no required state. Simple data holders, mixins, or marker classes work fine without __init__. The moment an instance must have certain attributes to be usable, you should define __init__.

python
class Point: def __init__(self, x, y): self.x = x self.y = y

Now Point(1, 2) works, and Point() raises TypeError because x and y are required. The default constructor is gone; you replaced it with one that enforces the state contract.

A common pattern is to provide default values so the constructor works with or without arguments:

python
class Point: def __init__(self, x=0, y=0): self.x = x self.y = y p1 = Point() p2 = Point(3, 4)

This is often what developers mean when they search for a "default constructor": a constructor with default parameter values. That is a different concept from the implicit object constructor, but the two are frequently conflated.

The Mutable Default Argument Pitfall

When you give __init__ default values, be careful with mutable defaults. A list or dictionary used as a default is created once at function definition time and shared across all instances.

python
class ShoppingCart: def __init__(self, items=[]): self.items = items cart1 = ShoppingCart() cart1.items.append("apple") cart2 = ShoppingCart() print(cart2.items) # ['apple']

Both carts share the same list. The fix is to use None as the default and create the list inside __init__:

python
class ShoppingCart: def __init__(self, items=None): self.items = items if items is not None else []

This keeps the default constructor behavior predictable: each instance gets its own list, and the constructor still works with zero arguments.

Runtime Cost and Maintainability Considerations

The default constructor has essentially no runtime cost beyond object allocation. object.__init__ performs no work, so there is no performance reason to avoid it. The maintainability concern is different: if you rely on the default constructor, every attribute assignment happens after construction, scattered across the calling code. That makes the class's required state implicit rather than explicit.

Defining __init__ centralizes initialization in one place. It also gives you a natural place to validate arguments, set derived attributes, and document what state a valid instance has. For a class that will be instantiated in many places, an explicit __init__ is usually the better choice even when all parameters have defaults.

The tradeoff is verbosity. A class with no __init__ is shorter, and for a simple namespace or a class used only for type checking, that brevity is reasonable. The decision depends on whether the class has meaningful state to enforce.

How the Default Constructor Interacts with Inheritance

When a subclass does not define __init__, it inherits the parent's __init__. In the common case where the subclass also keeps the default __new__, if the parent defines a constructor with required parameters, the subclass must accept those same parameters.

python
class Base: def __init__(self, name): self.name = name class Child(Base): pass c = Child("test") # works c = Child() # TypeError: __init__() missing 1 required positional argument: 'name'

If the subclass defines its own __init__ and needs the parent's initialization, it must call super().__init__() explicitly. Python does not call the parent's __init__ automatically. This is a common place where a missing super() call leaves parent attributes unset.

python
class Child(Base): def __init__(self, name, age): super().__init__(name) self.age = age

Understanding the default constructor means understanding that object.__init__ is the ultimate fallback. Any class whose method resolution order does not contain a more specific __init__ ends up at object.__init__, which accepts no additional arguments. In the common case where __new__ is also not customized, that is why such a class can only be instantiated with zero arguments.

Try matching browser tools

All tools

Small utilities that fit Python work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Python guides

More in Python

All Python guides

The guides filed next to this one in Python, in library order.