Object-Oriented Programming in Python
Alex Yarosh
Content Quality Analyst @ DataCamp
$$\Large{\text{New class functionality = Old class functionality + extra}}$$
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
: class whose functionality is being extended/inherited MyChild
: class that will inherit the functionality and add more# 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)
A SavingsAccount
is a BankAccount
(possibly with special features)
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
Object-Oriented Programming in Python