A class is a template (blueprint) for an object: it describes what data and actions the instances will have. The __init__ method is called automatically when an object is created and fills its attributes, while the first parameter self is a reference to the very instance being created.
- Group data and the actions on it in one place (an object).
- Create many similar objects from one template.
class Dog:
def __init__(self, name):
self.name = name
dog = Dog("Rex")
print(dog.name)Output:
RexDog("Rex") creates an object: __init__ is called automatically and stores the name in the attribute self.name. The attribute is accessed with a dot: dog.name.
Key rules:
- ✓A class describes an object; an instance is created by calling the class like a function.
- ✓__init__ — the constructor: runs when the object is created; self — a reference to the instance itself.
Common mistakes:
- !Forgetting self in methods — calling it causes an error.
- !Attributes are usually created in __init__, not on the fly.
A method is a function inside a class that works with a specific instance: it always receives self as the first parameter. The __str__ method defines how an object looks when printed via print().
- Describe the actions an object can perform (methods).
- Make printing objects readable instead of a memory address.
class Dog:
def __init__(self, name):
self.name = name
def bark(self):
return f"{self.name} is barking!"
dog = Dog("Rex")
print(dog.bark())Output:
Rex is barking!The bark method uses self.name from the same object. The call is via the dot: dog.bark().
class Dog:
def __init__(self, name):
self.name = name
def __str__(self):
return f"Dog {self.name}"
print(Dog("Rex"))Output:
Dog RexWithout __str__ print(obj) would show a memory address. __str__ returns the string that print outputs.
Key rules:
- ✓Instance methods always receive self as the first parameter.
- ✓__str__ defines how an object is printed via print().
Common mistakes:
- !A method without a return returns None.
- !__init__ creates the object, while __str__ only provides its text representation.
Inheritance lets you create a new class based on an existing one: the child class receives all the methods of the parent and can override them. super() calls a method of the parent class.
- Reuse code: the child class inherits the behavior of the parent.
- Add or change behavior without touching the parent class.
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
return "..."
class Cat(Animal):
def speak(self):
return "Meow!"
cat = Cat("Barsik")
print(cat.name, cat.speak())Output:
Barsik Meow!Cat inherits __init__ from Animal and overrides speak: the child version is called.
class Animal:
def __init__(self, name):
self.name = name
class Dog(Animal):
def __init__(self, name, age):
super().__init__(name)
self.age = age
dog = Dog("Rex", 3)
print(dog.name, dog.age)Output:
Rex 3super().__init__(name) calls the parent constructor to avoid duplicating code. The own attribute age is added after.
Key rules:
- ✓class Cat(Animal) — Cat inherits the methods and attributes of Animal.
- ✓An overridden child method replaces the parent one; super() calls the parent version.
Common mistakes:
- !Forgetting super().__init__(...) in the child — the parent attributes are not created.
- !Inheritance does not copy code: the child references the parent methods.