Skip to main content
  1. Blog/

Essential Object-Oriented Programming Summary in Python

·4 mins

📌 What is a function? #

A function is a reusable block of code that performs a specific task.

def greet(name):
    return f"Hello, {name}!"

print(greet("Anna"))

📌 What is a class? #

A class is a blueprint for creating objects. It defines attributes and methods.

class Animal:
    def __init__(self, species):
        self.species = species

    def make_sound(self):
        return "Generic sound"

dog = Animal("Dog")
print(dog.make_sound())

📌 What is an object? #

An object is an instance of a class. It contains data (attributes) and behaviors (methods).

class Person:
    def __init__(self, name):
        self.name = name

person1 = Person("Carlos")
print(person1.name)

📌 What is an entity, property, and attribute? #

  • Entity: a real-world or conceptual object (e.g., a car, a person).
  • Property: a characteristic of the entity (e.g., color, name).
  • Attribute: the code-level implementation of a property.
class Car:
    def __init__(self, brand, model):
        self.brand = brand  # property represented as an attribute
        self.model = model

📌 What is a method? #

A method is a function defined inside a class that operates on instances of that class.

class Person:
    def __init__(self, name):
        self.name = name

    def greet(self):
        return f"Hi, I'm {self.name}"

p = Person("Luis")
print(p.greet())

📌 What is a constructor? #

A constructor is a special method __init__ that runs when an object is created.

class User:
    def __init__(self, name):
        self.name = name

u = User("Valeria")
print(u.name)

📌 What is encapsulation? #

Encapsulation is about protecting the internal state of an object from outside interference.

class BankAccount:
    def __init__(self):
        self.__balance = 0  # private attribute

    def deposit(self, amount):
        self.__balance += amount

    def get_balance(self):
        return self.__balance

account = BankAccount()
account.deposit(100)
print(account.get_balance())

📌 What is a getter and a setter? #

In Object-Oriented Programming, getters and setters allow controlled access and modification of private attributes, respecting the principle of encapsulation.

  • Getter: method that returns the value of a private attribute.
  • Setter: method that allows modifying the value of a private attribute.

🧱 Classic way (works in any Python version)

class Product:
    def __init__(self):
        self.__price = 0

    def get_price(self):
        return self.__price

    def set_price(self, new_price):
        if new_price > 0:
            self.__price = new_price

# Usage
p = Product()
p.set_price(150)
print(p.get_price())  # 👉 150

Modern way with @property (recommended in Python)

class Product:
    def __init__(self):
        self.__price = 0

    @property
    def price(self):
        return self.__price

    @price.setter
    def price(self, new_price):
        if new_price > 0:
            self.__price = new_price


p = Product()
p.price = 150
print(p.price)  # 👉 150

Which way is better?

  • ✅ The modern way with @property is clearer, more readable, and more pythonic1. It allows using attributes as if they were public while maintaining internal control.
  • 🧱 The classic way is still useful in some environments or for beginners who want to be explicit.

📌 What is inheritance? #

Inheritance allows a class (child) to inherit attributes and methods from another class (parent).

class Vehicle:
    def start(self):
        return "Vehicle started"

class Car(Vehicle):
    def honk(self):
        return "Beep beep"

my_car = Car()
print(my_car.start())
print(my_car.honk())

📌 What is polymorphism? #

Polymorphism allows using the same method name with different behaviors depending on the object.

class Cat:
    def speak(self):
        return "Meow"

class Dog:
    def speak(self):
        return "Woof"

def make_speak(animal):
    print(animal.speak())

make_speak(Cat())
make_speak(Dog())

📌 What is abstraction? #

Abstraction means showing only essential features while hiding the details.

from abc import ABC, abstractmethod

class Shape(ABC):
    @abstractmethod
    def area(self):
        pass

class Square(Shape):
    def __init__(self, side):
        self.side = side

    def area(self):
        return self.side * self.side

s = Square(4)
print(s.area())

📌 What is an abstract class? #

An abstract class is a class that cannot be instantiated and requires subclasses to implement certain methods.

from abc import ABC, abstractmethod

class Animal(ABC):
    @abstractmethod
    def sound(self):
        pass

class Cow(Animal):
    def sound(self):
        return "Moo"

c = Cow()
print(c.sound())

📌 What is a decorator? #

A decorator is a function that modifies the behavior of another function or method.

def decorator(func):
    def new_function():
        print("Before execution")
        func()
        print("After execution")
    return new_function

@decorator
def greet():
    print("Hello world")

greet()

  1. It refers to writing code that follows Python’s style, best practices, and philosophy in a clear, readable, and elegant way. It’s not just about making the code work, but about making it look “natural” in Python. ↩︎

Gerardo Catalas
Author
Gerardo Catalas
I am a backend developer in training, interested in Python and SQL databases. I enjoy creating practical solutions and learning with every project I undertake.