Python Instantiation Mechanics: __new__, __init__, and Method Classification

Object Lifecycle Hooks

Two fundamental dunder methods govern how Python instances are brought into existence.

The __new__ Mechanism

Functioning as a static creator, __new__ handles the actual memory allocation. Its invoked before the instance is constructed and must return a valid object reference. Although typically inherited from the root object class, overriding this method enables advanced scenarios like immutability checks or custom factory behaviors.

The __init__ Hook

Executing immediately after __new__ completes, __init__ initializes the populated object. It receives the newly created instance via the self parameter. Its role is limited to configuring attributes and state; it does not construct the object itself and returns no value.

Component Responsibility Execution Order Return Value
__new__ Memory Allocation First Instance
__init__ State Initialization Second None

Method Scoping: Static vs. Instance

Methods define whether logic belongs to the class definition or a specific instance.

Static Methods Defined using the @staticmethod decorator, these functions operate independently of instance data. They can be called directly on the class name.

class Calculator:
    @staticmethod
    def get_pi():
        return 3.14159

# Call via class name
val = Calculator.get_pi()

Instance Methods These require an active object to execute and rely on the implicit self argument to manipulate object-specific data.

class Counter:
    def __init__(self):
        self.count = 0

    def step(self):
        self.count += 1

instance = Counter()
instance.step()

Static methods serve utility purposes without state interaction, whereas instance methods are designed for managing individual object states.

Tags: python object-oriented-programming magic-methods class-methods

Posted on Sat, 03 Oct 2026 16:45:55 +0000 by Gambler