๐Ÿ“ Python

Creating Classes: __init__ and self

P
Author
PyLand Team
๐Ÿ“…
Published
03.04.2026
โฑ๏ธ
Reading time
2 min
๐Ÿ‘๏ธ
Views
372
๐ŸŒฟ
Level
Medium

A class defines an object’s data and behavior, while __init__ establishes its initial state.

Basic Class Structure

class ClassName:
    def __init__(self, parameters):
        self.attribute = value

    def method(self):
        pass
class Zombie:
    def __init__(self, name, health):
        self.name = name
        self.health = health

    def groan(self):
        print(f"{self.name}: Graaah! ๐ŸงŸ")

walker = Zombie("Walker", 50)
walker.groan()  # Walker: Graaah! ๐ŸงŸ

__init__: The Constructor

__init__ is called automatically when an object is created. Analogy: when a baby is born, it’s immediately given a name and a date of birth โ€” __init__ does the same thing for objects.

class Human:
    def __init__(self, name, health, age):
        self.name = name
        self.health = health
        self.age = age
        self.kills = 0  # you can set a default value here

rick = Human("Rick", 100, 35)
print(rick.name)   # Rick
print(rick.kills)  # 0  (even though we didn't pass it!)

When you write Human("Rick", 100, 35), Python automatically calls __init__(self, "Rick", 100, 35).

self: A Reference to the Object

self is a reference to the specific object. When you write self.name, you’re saying: “the name attribute of this object.”

Without self it doesn’t work:

class Zombie:
    def __init__(self, name):
        name = name  # โŒ local variable, dies after __init__

walker = Zombie("Walker")
print(walker.name)  # AttributeError!

With self it works:

class Zombie:
    def __init__(self, name):
        self.name = name  # โœ… object attribute, persists

walker = Zombie("Walker")
print(walker.name)  # Walker

Each object stores its own attributes:

walker = Zombie("Walker", 50)
runner = Zombie("Runner", 30)

print(walker.name)   # Walker
print(runner.name)   # Runner  โ† its own attribute!
print(walker.health) # 50
print(runner.health) # 30

self in Methods

class Zombie:
    def __init__(self, name, health):
        self.name = name
        self.health = health

    def groan(self):
        print(f"{self.name}: Graaah!")  # self.name โ€” object attribute

    def take_damage(self, damage):
        self.health -= damage  # modifying the attribute
        print(f"{self.name} took {damage} damage. Health: {self.health}")

    def attack(self):
        self.groan()  # calling another method via self
        print(f"{self.name} attacks!")

walker = Zombie("Walker", 50)
walker.take_damage(20)  # Walker took 20 damage. Health: 30
walker.attack()         # Walker: Graaah! / Walker attacks!

Default Parameters

class Zombie:
    def __init__(self, name, health=50, speed=2):
        self.name = name
        self.health = health
        self.speed = speed

walker = Zombie("Walker")         # health=50, speed=2 (defaults)
runner = Zombie("Runner", 30, 5)  # health=30, speed=5

print(walker.health)  # 50
print(runner.speed)   # 5

Parameters with defaults must come after required parameters.

Common Mistakes

Mistake 1: Forgot self. โ€” the attribute won’t be saved

def __init__(self, name):
    name = name  # โŒ โ€” should be: self.name = name  โœ…

Mistake 2: Forgot self in method parameters

def __init__(name):        # โŒ no self โ€” TypeError
def __init__(self, name):  # โœ…

Mistake 3: Typo in __init__

def _init_(self, name):   # โŒ one underscore โ€” won't work
def __init__(self, name): # โœ… two underscores on each side

Mistake 4: Accessing an attribute without self inside a method

def groan(self):
    print(f"{name}: Graaah!")       # โŒ NameError
    print(f"{self.name}: Graaah!")  # โœ…

Summary

__init__ โ€” the constructor, called automatically when you write ClassName(arguments).

self โ€” a reference to the object; it lets you:
- save attributes: self.name = name
- read attributes in methods: print(self.name)
- call other methods: self.other_method()

class ClassName:
    def __init__(self, parameters):
        self.attribute = value

obj = ClassName(arguments)

Your reaction to the article

๐Ÿ’ฌ Comments (0)

๐Ÿ” Sign in to leave a comment
๐Ÿšช Login
๐Ÿ’ญ

No comments yet

Be the first to share your opinion about this article!

๐Ÿ”— Similar

Similar articles

Continue learning with these materials

๐Ÿ“

Event Loop in Python: How asyncio Enables Concurrโ€ฆ

Event loop is the heart of asyncio. It doesn't run code in parallel across multiple...

๐Ÿ“… 30.06.2026 ๐Ÿ‘๏ธ 386
๐Ÿ“

OOP: Program Like a World Builder ๐ŸŒ

Imagine: you're building a zombie-apocalypse game. You need zombies, humans, weapons. Every zombie has a...

๐Ÿ“… 03.04.2026 ๐Ÿ‘๏ธ 413
๐Ÿ“

The while Loop: Repeat While a Condition Holds ๐Ÿ”

The for loop works great when you know how many times to repeat an action....

๐Ÿ“… 30.03.2026 ๐Ÿ‘๏ธ 405
๐ŸŽ“ Continue learning

Courses that cover this material

Visit the course to apply this material in practice.

Python from Scratch: Build 7 Practical Projects Open course curriculum