Loading W Code...
Master Object-Oriented Programming with Python's clean and elegant syntax.
Learn how to define classes and create objects in Python
A class is a blueprint for creating objects. It defines attributes and methods.
An object is an instance of a class with actual values.
# Defining a class
class Car:
# Constructor (initializer)
def __init__(self, brand, color):
self.brand = brand # Instance attribute
self.color = color
self.speed = 0
# Instance method
def accelerate(self):
self.speed += 10
print(f"{self.brand} is now going {self.speed} km/h")
def brake(self):
self.speed = max(0, self.speed - 10)
print(f"{self.brand} slowed down to {self.speed} km/h")
# Creating objects
my_car = Car("Toyota", "Red")
your_car = Car("Honda", "Blue")
my_car.accelerate() # Toyota is now going 10 km/h
your_car.accelerate() # Honda is now going 10 km/h
class keyword to define a class__init__ is the constructor methodself refers to the current instancenew keyword needed to create objectsControl access to class attributes using naming conventions
Encapsulation is bundling data and methods together, restricting direct access to some components.
public: No underscore (accessible anywhere)_protected: Single underscore (convention, still accessible)__private: Double underscore (name mangling, harder to access)class BankAccount:
def __init__(self, account_number, initial_balance):
self.__account_number = account_number # Private
self.__balance = initial_balance # Private
# Getter using property decorator
@property
def balance(self):
return self.__balance
@property
def account_number(self):
return self.__account_number
# Methods for controlled modification
def deposit(self, amount):
if amount > 0:
self.__balance += amount
print(f"Deposited: {amount}")
def withdraw(self, amount):
if 0 < amount <= self.__balance:
self.__balance -= amount
print(f"Withdrawn: {amount}")
return True
print("Insufficient funds!")
return False
# Usage
account = BankAccount("1234567890", 1000)
print(account.balance) # 1000 (using getter)
account.deposit(500) # Deposited: 500
# account.__balance = 0 # This creates a new attribute, doesn't modify private one
class Circle:
def __init__(self, radius):
self._radius = radius
@property
def radius(self):
return self._radius
@radius.setter
def radius(self, value):
if value > 0:
self._radius = value
else:
raise ValueError("Radius must be positive")
@property
def area(self):
return 3.14159 * self._radius ** 2
circle = Circle(5)
print(circle.area) # 78.53975
circle.radius = 10 # Using setter
Create new classes that inherit from existing classes
Inheritance allows a class to inherit attributes and methods from another class.
# Parent class
class Animal:
def __init__(self, name, age):
self.name = name
self.age = age
def eat(self):
print(f"{self.name} is eating")
def sleep(self):
print(f"{self.name} is sleeping")
# Child class
class Dog(Animal):
def __init__(self, name, age, breed):
super().__init__(name, age) # Call parent constructor
self.breed = breed
def bark(self):
print(f"{self.name} says: Woof! Woof!")
# Override parent method
def eat(self):
print(f"{self.name} the {self.breed} is eating dog food")
# Usage
my_dog = Dog("Buddy", 3, "Golden Retriever")
my_dog.eat() # Overridden method
my_dog.sleep() # Inherited method
my_dog.bark() # Dog's own method
class Flyable:
def fly(self):
print("Flying!")
class Swimmable:
def swim(self):
print("Swimming!")
class Duck(Flyable, Swimmable):
def quack(self):
print("Quack!")
duck = Duck()
duck.fly() # From Flyable
duck.swim() # From Swimmable
duck.quack() # Duck's own
print(Duck.__mro__) # Shows the order Python looks for methods
Same interface, different implementations
Polymorphism allows objects of different classes to be treated as objects of a common base class.
Python uses "duck typing" - if it walks like a duck and quacks like a duck, it's a duck!
class Dog:
def speak(self):
return "Woof!"
class Cat:
def speak(self):
return "Meow!"
class Duck:
def speak(self):
return "Quack!"
# Polymorphism in action
def animal_sound(animal):
print(animal.speak())
# Works with any object that has a speak() method
animal_sound(Dog()) # Woof!
animal_sound(Cat()) # Meow!
animal_sound(Duck()) # Quack!
class Shape:
def area(self):
return 0
def draw(self):
print("Drawing a shape")
class Circle(Shape):
def __init__(self, radius):
self.radius = radius
def area(self):
return 3.14159 * self.radius ** 2
def draw(self):
print(f"Drawing a circle with radius {self.radius}")
class Rectangle(Shape):
def __init__(self, width, height):
self.width = width
self.height = height
def area(self):
return self.width * self.height
def draw(self):
print(f"Drawing a rectangle {self.width}x{self.height}")
# Polymorphism with a list
shapes = [Circle(5), Rectangle(4, 6), Circle(3)]
for shape in shapes:
shape.draw()
print(f"Area: {shape.area()}")
class Vector:
def __init__(self, x, y):
self.x = x
self.y = y
def __add__(self, other):
return Vector(self.x + other.x, self.y + other.y)
def __str__(self):
return f"Vector({self.x}, {self.y})"
v1 = Vector(2, 3)
v2 = Vector(4, 5)
v3 = v1 + v2 # Uses __add__
print(v3) # Vector(6, 8)
Abstract classes, interfaces, and Python special methods
from abc import ABC, abstractmethod
class Vehicle(ABC):
def __init__(self, brand):
self.brand = brand
@abstractmethod
def start(self):
pass
@abstractmethod
def stop(self):
pass
# Concrete method
def display_brand(self):
print(f"Brand: {self.brand}")
class Car(Vehicle):
def start(self):
print(f"{self.brand} car engine started")
def stop(self):
print(f"{self.brand} car engine stopped")
class ElectricCar(Vehicle):
def start(self):
print(f"{self.brand} electric car started silently")
def stop(self):
print(f"{self.brand} electric car powered down")
# vehicle = Vehicle("Generic") # Error! Can't instantiate abstract class
car = Car("Toyota")
car.start() # Toyota car engine started
class Book:
def __init__(self, title, author, pages):
self.title = title
self.author = author
self.pages = pages
# String representation
def __str__(self):
return f"{self.title} by {self.author}"
def __repr__(self):
return f"Book('{self.title}', '{self.author}', {self.pages})"
# Comparison
def __eq__(self, other):
return self.title == other.title and self.author == other.author
def __lt__(self, other):
return self.pages < other.pages
# Length
def __len__(self):
return self.pages
# Make it callable
def __call__(self):
return f"Reading {self.title}..."
book1 = Book("Python 101", "John", 300)
book2 = Book("Java Basics", "Jane", 400)
print(str(book1)) # Python 101 by John
print(len(book1)) # 300
print(book1 < book2) # True (300 < 400)
print(book1()) # Reading Python 101...
| Method | Purpose |
|---|---|
__init__ | Constructor |
__str__ | String for users |
__repr__ | String for developers |
__eq__ | Equality (==) |
__lt__, __gt__ | Less/Greater than |
__add__, __sub__ | +, - operators |
__len__ | len() function |
__getitem__ | [] indexing |
__iter__ | Make iterable |