Goal

You’ll be able to define a class and create objects from it, and understand what self actually represents — the single most confusing detail for most beginners learning classes.

Learn

A class is a blueprint for creating objects that bundle data and behavior together:

class Dog:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def bark(self):
        return f"{self.name} says woof!"

my_dog = Dog("Rex", 3)
print(my_dog.name)     # "Rex"
print(my_dog.bark())   # "Rex says woof!"

__init__ is a special method that runs automatically when a new object is created — it’s where you set up the object’s initial data (called attributes).

Here’s the genuinely important detail: self refers to the specific object being worked with — it’s what lets each object created from the same class keep its own independent data:

dog1 = Dog("Rex", 3)
dog2 = Dog("Milo", 5)
print(dog1.name)  # "Rex"
print(dog2.name)  # "Milo" — completely independent from dog1

When you call my_dog.bark(), Python automatically passes my_dog as the self parameter behind the scenes — you never pass it explicitly yourself when calling a method, only when defining one. This is exactly why every method inside a class needs self as its first parameter: it’s how that method knows which specific object’s data to actually work with.

Decision Task

You define a class Car with an __init__ that sets self.speed = 0, and a method accelerate that does self.speed += 10. Before reading on: if you create two separate Car objects and call accelerate() on only one of them, does the other car’s speed change too?

Show Answer

No — each object has its own independent copy of its attributes, thanks to self. Calling accelerate() on one specific Car object only modifies that object’s own self.speed; the second, separate Car object’s speed remains completely unaffected, since self always refers to whichever specific object the method was actually called on.

Common Mistake

Forgetting to include self as the first parameter when defining a method inside a class. Every regular method needs self as its first parameter (Python passes the calling object automatically), even though you never pass it explicitly yourself when calling the method — forgetting it in the definition causes a genuine, confusing error when the method is later called normally.

Practice Questions

1. Write a class called Book with an __init__ that sets title and author attributes.

Show Answer

class Book:\n def __init__(self, title, author):\n self.title = title\n self.author = author

2. What does self actually refer to inside a class method?

Show Answer

The specific object the method is currently being called on, letting each object keep its own independent data.

3. Given the Book class above, write code creating a Book object and printing its title.

Show Answer

my_book = Book("1984", "Orwell")\nprint(my_book.title)

4. True or False: when calling my_book.some_method(), you need to explicitly pass self as an argument yourself.

Show Answer

False — Python automatically passes the calling object (my_book) as self behind the scenes; you never pass it explicitly when calling a method, only when defining one.

5. What special method runs automatically when a new object is created from a class?

Show Answer

__init__

Try It Yourself

Without looking back, write a class called Rectangle with width and height attributes, and a method area() that returns width times height.

Show Answer

class Rectangle:\n def __init__(self, width, height):\n self.width = width\n self.height = height\n\n def area(self):\n return self.width * self.height

Quick Check

1. What is a class?

Show Answer

A blueprint for creating objects that bundle data and behavior together.

2. What does __init__ do?

Show Answer

Runs automatically when a new object is created, typically used to set up initial attributes.

3. What does self represent inside a class method?

Show Answer

The specific object the method is currently operating on.

4. Do two separate objects created from the same class share the same attribute values?

Show Answer

No — each object has its own independent copy of its attributes.

5. When calling a method on an object, do you pass self explicitly?

Show Answer

No — Python passes it automatically behind the scenes.

تحميل هذا الباب / Download this Chapterنسخة كاملة للدراسة بدون إنترنت، مع الأسئلة والإجابات والصور المتاحة.