Python – Object Oriented Programming – Design Inheritance & Properties

Best Practices Designing for Inheritance and Polymorphism Polymorphism   Interface # Withdraw amount from each account in list_of_accounts def batch_withdraw(list_of_accounts, amount): for acct in list_of_accounts: acct.withdraw(amount) b, c, s = BankAccount(1000), CheckingAccount(2000), SavingsAccount(3000) batch_withdraw([b,c,s]) # uses bankaccount.withdraw, checking, Savings batch_withdraw() doesn’t need to check the object to know which withdraw() to call Liskov substitution principle […]

Python – Object Oriented Programming – Exceptions

Integration with Python Operator overloading : comparison Object equality class Customer: def __init__(self, name, balance):, self.balance = name, balance cust1 = Customer(“John Fisher”, 2100) cust2 = Customer(“John Fisher”, 2100) cust1 == cust2 > False   Variables are references class Customer: def __init__(self, name, balance, id):, self.balance = name, balance = id cust1 […]

Python – Object Oriented Programming – Class Inheritance

Inheritance and Polymorphism Instance and Class Data Core Principles of OOP Inheritance Extending functionality of existing code Polymorphism Creating an unified interface Encapsulation Bundling of data and Methods Instance-level data class Employee: def __init__(self, name, salary): = name self.salary = salary emp1 = Employee(“Theo Mill”, 50000) emp2 = Employee(“Martina Kosova”, 65000) name , salary […]