# Classes, Instances & the self Parameter

> Understand Python object-oriented model: classes as blueprints, instance memory allocation, the explicit self parameter, instance dictionaries, and avoiding method signature errors.

Subject: Python | Lesson 13 of 24 | 7 min read
Interactive lesson: https://codewithanimation.in/python/classes-objects
Previous: [Lambda Functions & Higher-Order Pipelines](https://codewithanimation.in/python/lambda-functions.md) | Next: [Inheritance, Polymorphism & super()](https://codewithanimation.in/python/inheritance.md)

## Classes as Blueprints and Concrete Instances

In Python, a class is a blueprint for creating objects. You define a class using the `class` keyword followed by the class name in CamelCase and a colon.

An instance is a concrete realization of that class allocated in heap memory. When you call `car = Car('electric')`, Python creates a new object and invokes the class initializer `__init__` to configure its state.

Every instance maintains its own separate dictionary of attributes in `instance.__dict__`. Changes made to one instance do not affect other instances of the same class.

## The Explicit self Parameter

Unlike languages such as Java or C++ where the current instance is referenced implicitly via `this`, Python makes the instance reference completely explicit. By convention, the first parameter of any instance method is named `self`.

When you invoke a method on an instance using dot syntax `car.drive(60)`, Python translates this behind the scenes into `Car.drive(car, 60)`.

Because Python passes the instance as the first argument automatically, omitting `self` in the method signature causes runtime parameter count mismatches.

## Instance Attributes vs Class Attributes

Instance attributes are assigned to `self` inside `__init__` (e.g. `self.speed = 0`). Each instance owns an independent copy.

Class attributes are defined directly inside the class body outside any method. They are attached to the class object itself and are shared by all instances.

Class attributes are ideal for constants or tracking shared counts (for example, `species = 'Canis lupus'`). Be cautious with mutable class attributes like lists, as modifying them affects all instances simultaneously.

## Common mistake: The Missing self TypeError

A ubiquitous beginner error is writing an instance method without the `self` parameter: `def bark(): return 'Woof!'`.

When you call `dog.bark()`, Python automatically passes `dog` as the first argument. Because `bark()` was defined with zero parameters, Python raises: `TypeError: bark() takes 0 positional arguments but 1 was given`.

To resolve this, always include `self` as the first parameter: `def bark(self): return 'Woof!'`.

## Tip: __init__ is an Initializer, Not a Constructor

Technically, `__new__` is the constructor that allocates the raw object in memory, while `__init__` is the initializer that populates its attributes. Because `__init__` only initializes existing memory, it must always return `None`. Returning any other value from `__init__` raises a `TypeError`.

## Try it: The Class Blueprint & Instance Stamping Studio

Stamp out fresh instance objects from a class blueprint into heap memory. Observe how calling an instance method automatically binds and passes the instance reference pointer into the self parameter.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/python/classes-objects

## Check yourself

### Question 1

What happens when you call `dog.speak()` if `speak` is defined as `def speak(): return 'Arf!'` inside class Dog?

- A. TypeError: speak() takes 0 positional arguments but 1 was given
- B. The method returns 'Arf!' without error
- C. AttributeError: 'Dog' object has no attribute 'speak'
- D. Python prompts for a self argument via stdin

Correct answer: A

Calling `dog.speak()` translates to `Dog.speak(dog)`. Because `speak()` accepted zero parameters, receiving the `dog` argument triggers a TypeError.

- Why not B: Python automatically injects the instance as the first argument, which mismatches the 0-parameter definition.
- Why not C: The attribute exists on the class, but the function signature is invalid.
- Why not D: Python raises a TypeError immediately during method resolution.

### Question 2

Where are instance attributes stored in a standard Python object?

- A. In the instance's __dict__ attribute dictionary in heap memory
- B. In the global module namespace dictionary
- C. In a read-only CPython bytecode register
- D. In the parent class definition block

Correct answer: A

Each standard Python object holds a dictionary named `__dict__` mapping attribute names to their values in heap memory.

- Why not B: Instance attributes are encapsulated inside each object, not in module globals.
- Why not C: Standard Python instance attributes are mutable and reside in a heap dictionary.
- Why not D: The class holds methods and class attributes; instances maintain their own attribute state.

### Question 3

What must `__init__` return in a Python class?

- A. It must return None (or omit a return statement)
- B. It must return self explicitly
- C. It must return a boolean True indicating successful initialization
- D. It must return the class object

Correct answer: A

Python requires `__init__` to return `None`. Python's constructor mechanism (`__new__`) automatically supplies the newly created instance to the caller.

- Why not B: Returning self from __init__ raises a TypeError; Python returns the newly created instance automatically.
- Why not C: Returning any non-None value from __init__ raises a TypeError.
- Why not D: The initializer does not return the class.

## Remember this

- A class is a blueprint; an instance is an allocated object in heap memory with its own __dict__.
- The first parameter of any instance method must be self, representing the specific instance being operated on.
- Calling obj.method(arg) is syntactic sugar for Class.method(obj, arg).
- Instance attributes belong to self; class attributes are shared across all instances on the class object.
- __init__ initializes object state and must always return None.
