Объектно-ориентированное программирование на Python
Alex Yarosh
Content Quality Analyst @ DataCamp



$$\Large{\text{Функциональность нового класса = Функциональность старого класса + дополнение}}$$




class BankAccount:
def __init__(self, balance):
self.balance = balance
def withdraw(self, amount):
self.balance -= amount
# Empty class inherited from BankAccount
class SavingsAccount(BankAccount):
pass
class MyChild(MyParent):
# Do stuff here
MyParent: класс, функциональность которого расширяется/наследуетсяMyChild: класс, который унаследует функциональность и добавит новую# Constructor inherited from BankAccount
savings_acct = SavingsAccount(1000)
type(savings_acct)
__main__.SavingsAccount
# Attribute inherited from BankAccount
savings_acct.balance
1000
# Method inherited from BankAccount
savings_acct.withdraw(300)
SavingsAccount является BankAccount
(возможно, с дополнительными возможностями)
savings_acct = SavingsAccount(1000)
isinstance(savings_acct, SavingsAccount)
True
isinstance(savings_acct, BankAccount)
True
acct = BankAccount(500)
isinstance(acct,SavingsAccount)
False
isinstance(acct,BankAccount)
True
Объектно-ориентированное программирование на Python