0% found this document useful (0 votes)
12 views35 pages

Python OOP: Classes and Inheritance

The document provides an overview of Object-Oriented Programming (OOP) principles in Python, including concepts like inheritance, polymorphism, and encapsulation. It explains instance-level and class-level data, class attributes, class methods, and alternative constructors, along with practical examples. Additionally, it discusses customizing functionality through inheritance and the importance of code reuse in programming.

Uploaded by

yingchenyeh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views35 pages

Python OOP: Classes and Inheritance

The document provides an overview of Object-Oriented Programming (OOP) principles in Python, including concepts like inheritance, polymorphism, and encapsulation. It explains instance-level and class-level data, class attributes, class methods, and alternative constructors, along with practical examples. Additionally, it discusses customizing functionality through inheritance and the importance of code reuse in programming.

Uploaded by

yingchenyeh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Instance and class

data
O B J E C T- O R I E N T E D P R O G R A M M I N G I N P Y T H O N

Alex Yarosh
Content Quality Analyst @ DataCamp
Core principles of OOP

Inheritance:
Extending functionality of existing code

Polymorphism:
Creating a unified interface

Encapsulation:
Bundling of data and methods

OBJECT-ORIENTED PROGRAMMING IN PYTHON


Instance-level data
class Employee:
def __init__(self, name, salary):
[Link] = name
[Link] = salary

emp1 = Employee("Teo Mille", 50000)


emp2 = Employee("Marta Popov", 65000)

name , salary are instance attributes

self binds to an instance

OBJECT-ORIENTED PROGRAMMING IN PYTHON


Class-level data
Data shared among all instances of a class
Define class attributes in the body of class

class MyClass:
# Define a class attribute
CLASS_ATTR_NAME = attr_value

"Global variable" within the class

OBJECT-ORIENTED PROGRAMMING IN PYTHON


Class-level data
class Employee: MIN_SALARY is shared among all instances
# Define a class attribute
Don't use self to define class attribute
MIN_SALARY = 30000 #<--- no self.
def __init__(self, name, salary): use ClassName.ATTR_NAME to access the
[Link] = name
class attribute value
# Use class name to access class attribute
if salary >= Employee.MIN_SALARY:
[Link] = salary
else:
[Link] = Employee.MIN_SALARY

OBJECT-ORIENTED PROGRAMMING IN PYTHON


Class-level data
class Employee: emp1 = Employee("TBD", 40000)
# Define a class attribute print(emp1.MIN_SALARY)
MIN_SALARY = 30000
def __init__(self, name, salary):
30000
[Link] = name
# Use class name to access class attribute
emp2 = Employee("TBD", 60000)
if salary >= Employee.MIN_SALARY:
print(emp2.MIN_SALARY)
[Link] = salary
else:
[Link] = Employee.MIN_SALARY 30000

OBJECT-ORIENTED PROGRAMMING IN PYTHON


Why use class attributes?
Global constants related to the class

minimal/maximal values for attributes

commonly used values and constants, e.g. pi for a Circle class

...

OBJECT-ORIENTED PROGRAMMING IN PYTHON


Class methods
Methods are already "shared": same code for every instance
Class methods can't use instance-level data

class MyClass:

@classmethod # <---use decorator to declare a class method


def my_awesome_method(cls, args...): # <---cls argument refers to the class
# Do stuff here
# Can't use any instance attributes :(

MyClass.my_awesome_method(args...)

OBJECT-ORIENTED PROGRAMMING IN PYTHON


Alternative constructors
class Employee: Can only have one __init__()
MIN_SALARY = 30000
def __init__(self, name, salary=30000):
[Link] = name
if salary >= Employee.MIN_SALARY:
[Link] = salary
else:
[Link] = Employee.MIN_SALARY

Use class methods to create objects


@classmethod
def from_file(cls, filename): Use return to return an object
with open(filename, "r") as f:
cls(...) will call __init__(...)
name = [Link]()
return cls(name)

OBJECT-ORIENTED PROGRAMMING IN PYTHON


Alternative constructors
class Employee:
MIN_SALARY = 30000
def __init__(self, name, salary=30000):
[Link] = name
if salary >= Employee.MIN_SALARY:
[Link] = salary
else:
# Create an employee without calling Employee()
[Link] = Employee.MIN_SALARY
emp = Employee.from_file("employee_data.txt")
type(emp)
@classmethod
def from_file(cls, filename):
with open(filename, "r") as f: __main__.Employee
name = [Link]()
return cls(name)

OBJECT-ORIENTED PROGRAMMING IN PYTHON


Let's practice!
O B J E C T- O R I E N T E D P R O G R A M M I N G I N P Y T H O N
Class inheritance
O B J E C T- O R I E N T E D P R O G R A M M I N G I N P Y T H O N

Alex Yarosh
Content Quality Analyst @ DataCamp
Code reuse

OBJECT-ORIENTED PROGRAMMING IN PYTHON


Code reuse

1. Someone has already done it

Modules are great for fixed functionality

OOP is great for customizing functionality

OBJECT-ORIENTED PROGRAMMING IN PYTHON


Code reuse

1. Someone has already done it

2. DRY: Don't Repeat Yourself

OBJECT-ORIENTED PROGRAMMING IN PYTHON


Code reuse

1. Someone has already done it

2. DRY: Don't Repeat Yourself

OBJECT-ORIENTED PROGRAMMING IN PYTHON


Inheritance

New class functionality = Old class functionality + extra

OBJECT-ORIENTED PROGRAMMING IN PYTHON


OBJECT-ORIENTED PROGRAMMING IN PYTHON
OBJECT-ORIENTED PROGRAMMING IN PYTHON
OBJECT-ORIENTED PROGRAMMING IN PYTHON
OBJECT-ORIENTED PROGRAMMING IN PYTHON
Implementing class inheritance
class BankAccount: class MyChild(MyParent):
def __init__(self, balance): # Do stuff here
[Link] = balance
MyParent : class whose functionality is
def withdraw(self, amount): being extended/inherited
[Link] -= amount
MyChild : class that will inherit the
functionality and add more
# Empty class inherited from BankAccount
class SavingsAccount(BankAccount):
pass

OBJECT-ORIENTED PROGRAMMING IN PYTHON


Child class has all of the the parent data
# 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)

OBJECT-ORIENTED PROGRAMMING IN PYTHON


Inheritance: "is-a" relationship
A SavingsAccount is a BankAccount

(possibly with special features)

savings_acct = SavingsAccount(1000) acct = BankAccount(500)


isinstance(savings_acct, SavingsAccount) isinstance(acct,SavingsAccount)

True False

isinstance(savings_acct, BankAccount) isinstance(acct,BankAccount)

True True

OBJECT-ORIENTED PROGRAMMING IN PYTHON


Let's practice!
O B J E C T- O R I E N T E D P R O G R A M M I N G I N P Y T H O N
Customizing
functionality via
inheritance
O B J E C T- O R I E N T E D P R O G R A M M I N G I N P Y T H O N

Alex Yarosh
Content Quality Analyst @ DataCamp
OBJECT-ORIENTED PROGRAMMING IN PYTHON
What we have so far
class BankAccount:
def __init__(self, balance):
[Link] = balance

def withdraw(self, amount):


[Link] -=amount

# Empty class inherited from BankAccount


class SavingsAccount(BankAccount):
pass

OBJECT-ORIENTED PROGRAMMING IN PYTHON


Customizing constructors
class SavingsAccount(BankAccount):

# Constructor specifically for SavingsAccount with an additional parameter


def __init__(self, balance, interest_rate):
# Call the parent constructor using ClassName.__init__()
BankAccount.__init__(self, balance) # <--- self is a SavingsAccount but also a BankAccount
# Add more functionality
self.interest_rate = interest_rate

Can run constructor of the parent class first by Parent.__init__(self, args...)

Add more functionality

Don't have to call the parent constructors

OBJECT-ORIENTED PROGRAMMING IN PYTHON


Create objects with a customized constructor
# Construct the object using the new constructor
acct = SavingsAccount(1000, 0.03)
acct.interest_rate

0.03

OBJECT-ORIENTED PROGRAMMING IN PYTHON


Adding functionality
Add methods as usual
Can use the data from both the parent and the child class

class SavingsAccount(BankAccount):

def __init__(self, balance, interest_rate):


BankAccount.__init__(self, balance)
self.interest_rate = interest_rate

# New functionality
def compute_interest(self, n_periods = 1):
return [Link] * ( (1 + self.interest_rate) ** n_periods - 1)

OBJECT-ORIENTED PROGRAMMING IN PYTHON


OBJECT-ORIENTED PROGRAMMING IN PYTHON
Customizing functionality
class CheckingAccount(BankAccount): Can change the signature (add
def __init__(self, balance, limit): parameters)
BankAccount.__init__(self, balance)
[Link] = limit Use [Link](self, args...) to call
def deposit(self, amount):
[Link] += amount
a method from the parent class
def withdraw(self, amount, fee=0):
if fee <= [Link]:
[Link](self, amount + fee)
else:
[Link](self,
amount + [Link])

OBJECT-ORIENTED PROGRAMMING IN PYTHON


check_acct = CheckingAccount(1000, 25) bank_acct = BankAccount(1000)

# Will call withdraw from CheckingAccount # Will call withdraw from BankAccount
check_acct.withdraw(200) bank_acct.withdraw(200)

# Will call withdraw from CheckingAccount # Will produce an error


check_acct.withdraw(200, fee=15) bank_acct.withdraw(200, fee=15)

TypeError: withdraw() got an unexpected


keyword argument 'fee'

OBJECT-ORIENTED PROGRAMMING IN PYTHON


Let's practice!
O B J E C T- O R I E N T E D P R O G R A M M I N G I N P Y T H O N

You might also like