This post of series is based on Udemy FastAPI - The Complete Course 2025 by Eric Roby
Object Oriented Programming(OOP) is a programming pradadigm based on the concepts of objects, which can contain data and code.
Abstraction means to hide the implementation and only show necessary details to the user.
# Enemy.py
class Enemy:
type_of_enemy: str
health_points: int = 10
attack_damage: int = 1
def talk(self):
print(f"I am a {self.type_of_enemy}. Be prepared to fight!")
# main.py
from Enemy import *
zombie = Enemy()
zombie.type_of_enemy = 'Zombie'
print(f'{zombie.type_of_enemy} has {zombie.health_points} health points and can do attack of {zombie.attack_damage}')
zombie.talk()
Constructors are used to create and initialize an object of a class with or without starting values.
from Enemy import *
enemy = Enemy() # calling a constructor
self resfers to the current class or object.
class Enemy:
type_of_enemy: str
health_points: int = 10
attack_damage: int = 1
def __init__(self):
pass
==> Automatically created if no constructor is found.
class Enemy:
type_of_enemy: str
health_points: int = 10
attack_damage: int = 1
def __init__(self):
print("I'm a enemy")
class Enemy:
type_of_enemy: str
health_points: int = 10
attack_damage: int = 1
def __init__(self, type_of_enemy, health_points = 10, attack_damage = 1):
self.type_of_enemy = type_of_enemy
self.health_points = health_points
self.attack_damage = attack_damage
---
zombie = Enemy('Zombie', 10, 1)