Python OOP Foundation
class / self Topic 09 Foundation

Objects group data and behavior together

Foundation lesson for Python OOP. Learn how classes blueprint objects, and how objects bundle state (data) with methods (behavior).

Picture this: blueprint → object

class is the blueprint. Calling Class() builds one object with its own data.

class Dogblueprint
→
Dog('Rex')build one
→
rex.namedata
→
rex.bark()behavior

In easy words: What is a class?

A class is a blueprint. It describes what properties and actions an object can have. When you create an object from that blueprint, it is called an instance.

Diagram — class blueprint → objects

class Dog blueprint Dog("Rex") Dog("Mia") methods / self behavior on each object

Where OOP is used

OOP models real entities: a User has a name and email; a Car has speed and fuel; a BankAccount has balance and methods to deposit/withdraw. Games, apps, and large systems all use OOP to organize code.

UML Class Diagram

Dog
- name: str
- age: int
+ __init__(name, age)
+ bark()

Class contains both attributes (state) and methods (behavior).

Object Relationship View

Class: Dog
creates
dog1: Dog
and
dog2: Dog

Multiple objects can be instantiated from a single class blueprint.

class keyword

  • class Dog: — defines a blueprint
  • Properties stored in self.name, self.age

__init__ method

  • def __init__(self, name): — constructor, runs when object is created
  • Set up initial state (like a birth certificate)

Class syntax highlight

1class Dog:
2def __init__(self, name):
3self.name = name
4def bark(self):
5print(f"{self.name} barks!")

Real world: creating a dog

dog1 = Dog("Buddy") creates one instance called dog1, with name="Buddy".
dog2 = Dog("Max") creates another instance called dog2, with name="Max". Each object has its own name and can bark independently.

dog = Dog("Buddy")

Press "Run one step" to see class behavior

Example Program

1class Dog:
2def __init__(self, name):
3self.name = name
4def bark(self):
5print(f"{self.name} barks")
6
7dog = Dog("Max")
8dog.bark()

Object-Oriented Python — Topic Quiz

Check understanding

Answer all questions, then click Check answers. Use this before Practice in the editor.

Q1. A class is best described as…

Q2. What is self?

Q3. Inheritance means…

Q4. Encapsulation mainly…

Score: —