Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
24 views52 pages

10b.classes Inheritance, Importing Classes

The document describes the implementation of a Car class and its subclass ElectricCar in Python, detailing attributes, methods, and inheritance. It explains how the ElectricCar inherits characteristics from the Car class while also introducing its own attributes, such as battery size. Additionally, it covers method overriding and the use of instances as attributes, specifically how the ElectricCar can include a Battery instance.
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)
24 views52 pages

10b.classes Inheritance, Importing Classes

The document describes the implementation of a Car class and its subclass ElectricCar in Python, detailing attributes, methods, and inheritance. It explains how the ElectricCar inherits characteristics from the Car class while also introducing its own attributes, such as battery size. Additionally, it covers method overriding and the use of instances as attributes, specifically how the ElectricCar can include a Battery instance.
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
You are on page 1/ 52

Inheritance, Importing Classes

Car Class
class Car:
class Car: car.py
def __init__(self, make, model, year): car.py
def __init__(self, make, model, year):
self.make = make
self.make = make
self.model = model
self.model = model
self.year = year
self.year = year
self.odometer_reading = 0
self.odometer_reading = 0

def get_descriptive_name(self):
def get_descriptive_name(self):
long_name = f"{self.year} {self.manufacturer} {self.model}"
long_name = f"{self.year} {self.manufacturer} {self.model}"
return long_name.title()
return long_name.title()

def read_odometer(self):
def read_odometer(self):
print(f"This car has {self.odometer_reading} miles on it.")
print(f"This car has {self.odometer_reading} miles on it.")

def update_odometer(self, mileage):


def update_odometer(self, mileage):
if mileage >= self.odometer_reading:
if mileage >= self.odometer_reading:
self.odometer_reading = mileage
self.odometer_reading = mileage
else:
else:
print("You can't roll back an odometer!")
print("You can't roll back an odometer!")

def increment_odometer(self, miles):


def increment_odometer(self, miles):
self.odometer_reading += miles
self.odometer_reading += miles
Importing Car Class
from
from car
car import
import Car
Car my_car.py
my_car.py

my_new_car
my_new_car == Car('audi',
Car('audi', 'a4',
'a4', 2019)
2019)
print(my_new_car.get_descriptive_name())
print(my_new_car.get_descriptive_name())
my_new_car.odometer_reading
my_new_car.odometer_reading == 23
23
my_new_car.read_odometer()
my_new_car.read_odometer()
Inheritance – Electric Car
class
class Car:
Car:

--
-- same
same as
as before
before --
--

class
class ElectricCar(Car):
ElectricCar(Car):
"""Represent
"""Represent aspects
aspects of
of aa car,
car, specific
specific to
to electric
electric vehicles."""
vehicles."""
Inheritance – Electric Car
class
class Car:
Car:

--
-- same
same as
as before
before --
--

class
class ElectricCar(Car):
ElectricCar(Car):
"""Represent
"""Represent aspects
aspects of
of aa car,
car, specific
specific to
to electric
electric vehicles."""
vehicles."""

def
def __init__(self,
__init__(self, make,
make, model,
model, year):
year):
"""Initialize
"""Initialize attributes
attributes ofof the
the parent
parent class."""
class."""
super().__init__(make,
super().__init__(make, model,
model, year)
year)
Inheritance – Electric Car
class
class Car:
Car:

--
-- same
same as
as before
before --
--

class
class ElectricCar(Car):
ElectricCar(Car):
"""Represent
"""Represent aspects
aspects of
of aa car,
car, specific
specific to
to electric
electric vehicles."""
vehicles."""

def
def __init__(self,
__init__(self, make,
make, model,
model, year):
year):
"""Initialize
"""Initialize attributes
attributes ofof the
the parent
parent class."""
class."""
super().__init__(make,
super().__init__(make, model,
model, year)
year)

super(
super() )calls
callsaamethod
methodfrom
fromthe
theparent
parentclass
class
Calling
Callingthe
the__init__(
__init__() )method
methodfrom
fromCar
Car----gives
givesan
anElectricCar
ElectricCarinstance
instanceall
allthe
theattributes
attributes
defined
definedininparent’s
parent’s__init__(
__init__() )
Parent
Parentisisthe
thesuperclass
superclassand
andChild
Childclass
classisisthe
thesubclass
subclass
Inheritance – Electric Car
class
class Car:
Car:

--
-- same
same as
as before
before --
--

class
class ElectricCar(Car):
ElectricCar(Car):
"""Represent
"""Represent aspects
aspects of
of aa car,
car, specific
specific to
to electric
electric vehicles."""
vehicles."""

def
def __init__(self,
__init__(self, make,
make, model,
model, year):
year):
"""Initialize
"""Initialize attributes
attributes ofof the
the parent
parent class."""
class."""
super().__init__(make,
super().__init__(make, model,
model, year)
year)

my_tesla
my_tesla == ElectricCar('tesla',
ElectricCar('tesla', 'model
'model s',
s', 2019)
2019)
print(my_tesla.get_descriptive_name())
print(my_tesla.get_descriptive_name())
Inheritance

When creating a specialized version of another class, use
inheritance

When one class inherits from another, it takes on the
attributes and methods of the first class.

Parent class, child class

Child class inherits any or all of the attributes and methods of
its parent class and defines new attributes and methods of its
own
Attributes and Methods for Child Class
class
class Car:
Car:

--
-- same
same as
as before
before --
--

class
class ElectricCar(Car):
ElectricCar(Car):
"""Represent
"""Represent aspects
aspects of
of aa car,
car, specific
specific to
to electric
electric vehicles."""
vehicles."""

def
def __init__(self,
__init__(self, make,
make, model,
model, year):
year):
"""Initialize
"""Initialize attributes
attributes ofof the
the parent
parent class."""
class."""
super().__init__(make,
super().__init__(make, model,
model, year)
year)
Attributes and Methods for Child Class
class
class Car:
Car:

--
-- same
same as
as before
before --
--

class
class ElectricCar(Car):
ElectricCar(Car):
def
def __init__(self, make,
__init__(self, make, model,
model, year):
year):
"""
"""
Initialize
Initialize attributes
attributes of
of the
the parent
parent class.
class.
Then
Then initialize
initialize attributes
attributes specific
specific to
to an
an electric
electric car.
car.
"""
"""
super().__init__(make,
super().__init__(make, model,
model, year)
year)
self.battery_size = 75
self.battery_size = 75
Attributes and Methods for Child Class
class
class Car:
Car:

--
-- same
same as
as before
before --
--

class
class ElectricCar(Car):
ElectricCar(Car):
def
def __init__(self, make,
__init__(self, make, model,
model, year):
year):
"""
"""
Initialize
Initialize attributes
attributes of
of the
the parent
parent class.
class.
Then
Then initialize
initialize attributes
attributes specific
specific to
to an
an electric
electric car.
car.
"""
"""
super().__init__(make,
super().__init__(make, model,
model, year)
year)
self.battery_size = 75
self.battery_size = 75

def
def describe_battery(self):
describe_battery(self):
"""Print
"""Print aa statement
statement describing
describing the
the battery
battery size."""
size."""
print(f"This
print(f"This car
car has
has aa {self.battery_size}-kWh
{self.battery_size}-kWh battery."’
battery."’
Attributes and Methods for Child Class
class Car:
class Car:

-- same as before --
-- same as before --

class ElectricCar(Car):
class ElectricCar(Car):
def __init__(self, make, model, year):
def __init__(self, make, model, year):
"""
"""
Initialize
Initialize attributes
attributes of
of the
the parent
parent class.
class.
Then
Then initialize
initialize attributes
attributes specific
specific to
to an
an electric
electric car.
car.
"""
"""
super().__init__(make,
super().__init__(make, model,
model, year)
year)
self.battery_size = 75
self.battery_size = 75

def describe_battery(self):
def describe_battery(self):
"""Print a statement describing the battery size."""
"""Print a statement describing the battery size."""
print(f"This car has a {self.battery_size}-kWh battery."
print(f"This car has a {self.battery_size}-kWh battery."

my_tesla
my_tesla == ElectricCar('tesla',
ElectricCar('tesla', 'model
'model s',
s', 2019)
2019)
print(my_tesla.get_descriptive_name())
print(my_tesla.get_descriptive_name())
my_tesla.describe_battery()name())
my_tesla.describe_battery()name())
Overriding methods from Parent Class
class
class Car:
Car:

--
-- same
same as
as before
before --
--

class
class ElectricCar(Car):
ElectricCar(Car):
--
-- same
same as
as before
before --
--

def
def fill_gas_tank(self):
fill_gas_tank(self):
"""Electric
"""Electric cars
cars don't
don't have
have gas
gas tanks."""
tanks."""
print("This
print("This car
car doesn't
doesn't need
need aa gas
gas tank!")
tank!")
Overriding methods from Parent Class

Say the parent class had a class
class Car:
Car:

method fill_gas_tank() --
-- same
same as
as before
before --
--


Defining a method of the same class
class ElectricCar(Car):
ElectricCar(Car):
name in the child class will --
-- same as
same as before
before --
--

override the behaviour of the def


def fill_gas_tank(self):
fill_gas_tank(self):
parent’s method """Electric
"""Electric cars
print("This
cars don't
don't have
have gas
gas tanks."""
tanks."""
print("This car
car doesn't
doesn't need
need aa gas
gas tank!")
tank!")
Overriding methods from Parent Class

Say the parent class had a method class
class Car:
Car:

fill_gas_tank() --
-- same
same as
as before
before --
--

Defining a method of the same class
class ElectricCar(Car):
ElectricCar(Car):
name in the child class will override --
-- same as
same as before
before --
--
the behaviour of the parent’s method
def
def fill_gas_tank(self):
fill_gas_tank(self):

Calling the fill_gas_tank() method in """Electric
"""Electric cars
cars don't
don't have
have gas
gas tanks."""
tanks."""
an ElectricCar object will run this print("This
print("This car
car doesn't
doesn't need
need aa gas
gas tank!")
tank!")

code

Inheritance: child class retains what
is needed and overrides everything
else from the parent class.
Instances as Attributes
class Car:
class Car:
-- same as before --
-- same as before --

class Battery:
class Battery:
"""A simple attempt to model a battery for an electric car."""
"""A simple attempt to model a battery for an electric car."""
def __init__(self, battery_size=75):
def __init__(self, battery_size=75):
"""Initialize the battery's attributes."""
"""Initialize the battery's attributes."""
self.battery_size = battery_size
self.battery_size = battery_size
def describe_battery(self):
def describe_battery(self):
"""Print a statement describing the battery size."""
"""Print a statement describing the battery size."""
print(f"This car has a {self.battery_size}-kWh battery")
print(f"This car has a {self.battery_size}-kWh battery")

class ElectricCar(Car):
class ElectricCar(Car):
"""Represent aspects of a car, specific to electric vehicles."""
"""Represent aspects of a car, specific to electric vehicles."""
def __init__(self, make, model, year):
def __init__(self, make, model, year):
"""
"""
Initialize attributes of the parent class.
Initialize attributes of the parent class.
Then initialize attributes specific to an electric car.
Then initialize attributes specific to an electric car.
"""
"""
super().__init__(make, model, year)
super().__init__(make, model, year)
self.battery = Battery()
self.battery = Battery()
Instances as Attributes
class Car:
class Car:
-- same as before --
-- same as before --

class Battery:
class Battery:
"""A simple attempt to model a battery for an electric car."""
"""A simple attempt to model a battery for an electric car."""
def __init__(self, battery_size=75):
def __init__(self, battery_size=75):
"""Initialize the battery's attributes."""
"""Initialize the battery's attributes."""
self.battery_size = battery_size
self.battery_size = battery_size
def describe_battery(self):
def describe_battery(self):
"""Print a statement describing the battery size."""
"""Print a statement describing the battery size."""
print(f"This car has a {self.battery_size}-kWh battery")
print(f"This car has a {self.battery_size}-kWh battery")

class ElectricCar(Car):
class ElectricCar(Car):
"""Represent aspects of a car, specific to electric vehicles."""
"""Represent aspects of a car, specific to electric vehicles."""
def __init__(self, make, model, year):
def __init__(self, make, model, year):
"""
"""
Initialize attributes of the parent class.
Initialize attributes of the parent class.
Then initialize attributes specific to an electric car.
Then initialize attributes specific to an electric car.
"""
"""
super().__init__(make, model, year)
super().__init__(make, model, year) my_tesla
my_tesla == ElectricCar('tesla',
ElectricCar('tesla', 'model
'model s',
s', 2019)
2019)
self.battery = Battery()
self.battery = Battery() print(my_tesla.get_descriptive_name())
print(my_tesla.get_descriptive_name())
my_tesla.battery.describe_battery()
my_tesla.battery.describe_battery()
class
class Car:
--
Car:
-- same
same as
as before
before --
--
Instances as Attributes
class
class Battery:
Battery:
--
-- same
same as
as before
before --
--
def
def get_range(self):
get_range(self):
"""Print
"""Print aa statement
statement about
about the
the range
range this
this battery
battery provides."""
provides."""
if
if self.battery_size
self.battery_size ==
== 75:
75:
range
range == 260
260
elif
elif self.battery_size
self.battery_size ==
== 100:
100:
range
range == 315
315

print(f"This
print(f"This car
car can
can go
go about
about {range}
{range} miles
miles on
on aa full
full charge.")
charge.")

class
class ElectricCar(Car):
ElectricCar(Car):
--
-- same
same as
as before
before --
--
class
class Car:
--
Car:
-- same
same as
as before
before --
--
Instances as Attributes
class
class Battery:
Battery:
--
-- same
same as
as before
before --
--
def
def get_range(self):
get_range(self):
"""Print
"""Print aa statement
statement about
about the
the range
range this
this battery
battery provides."""
provides."""
if
if self.battery_size
self.battery_size ==
== 75:
75:
range
range == 260
260
elif
elif self.battery_size
self.battery_size ==
== 100:
100:
range
range == 315
315

print(f"This
print(f"This car
car can
can go
go about
about {range}
{range} miles
miles on
on aa full
full charge.")
charge.")

class my_tesla
class ElectricCar(Car):my_tesla == ElectricCar('tesla',
ElectricCar(Car): ElectricCar('tesla', 'model
'model s',
s', 2019)
2019)
--
-- same
same as
as before --
beforeprint(my_tesla.get_descriptive_name())
--
print(my_tesla.get_descriptive_name())
my_tesla.battery.describe_battery()
my_tesla.battery.describe_battery()
my_tesla.battery.get_range()
my_tesla.battery.get_range()
Parent and Subclass in the same file
class
class Car:
Car: Car
-- car.py
car.py
-- same
same as
as before
before --
--

class
class ElectricCar(Car):
ElectricCar(Car): ElectricCar
--
-- same
same as
as before
before --
--
Importing subclass car.py
car.py
"""A
"""A set
set of
of classes
classes used
used to
to represent
represent gas
gas and
and electric
electric cars."""
cars.""" Car
class
class Car:
Car:
--
-- same
same as
as before
before --
--
ElectricCar

class
class ElectricCar(Car):
ElectricCar(Car):
--
-- same
same as
as before
before --
--

my_electric_cars.py
my_electric_cars.py
from
from car
car import
import ElectricCar
ElectricCar

my_tesla
my_tesla == ElectricCar('tesla',
ElectricCar('tesla', 'model
'model s',
s', 2019)
2019)
print(my_tesla.get_descriptive_name())
print(my_tesla.get_descriptive_name())
my_tesla.battery.describe_battery()
my_tesla.battery.describe_battery()
my_tesla.battery.get_range()
my_tesla.battery.get_range()
Importing Multiple Subclasses car.py
car.py
"""A
"""A set
set of
of classes
classes used
used to
to represent
represent gas
gas and
and electric
electric cars."""
cars.""" Car
class
class Car:
Car:
--
-- same
same as
as before
before --
-- ElectricCar

class
class ElectricCar(Car):
ElectricCar(Car):
--
-- same
same as
as before
before --
--

my_cars.py
my_cars.py
from
from car
car import
import Car,
Car, ElectricCar
ElectricCar

my_beetle
my_beetle == Car('volkswagen',
Car('volkswagen', 'beetle',
'beetle', 2019)
2019)
print(my_beetle.get_descriptive_name())
print(my_beetle.get_descriptive_name())

my_tesla
my_tesla == ElectricCar('tesla',
ElectricCar('tesla', 'roadster',
'roadster', 2019)
2019)
print(my_tesla.get_descriptive_name())
print(my_tesla.get_descriptive_name())
Importing Entire Module car.py
car.py
"""A
"""A set
set of
of classes
classes used
used to
to represent
represent gas
gas and
and electric
electric cars."""
cars.""" Car
class
class Car:
Car:
--
-- same
same as
as before
before --
-- ElectricCar

class
class ElectricCar(Car):
ElectricCar(Car):
--
-- same
same as
as before
before --
--

my_cars.py
my_cars.py
import
import car
car

my_beetle
my_beetle == car.Car('volkswagen',
car.Car('volkswagen', 'beetle',
'beetle', 2019)
2019)
print(my_beetle.get_descriptive_name())
print(my_beetle.get_descriptive_name())

my_tesla
my_tesla == car.ElectricCar('tesla',
car.ElectricCar('tesla', 'roadster',
'roadster', 2019)
2019)
print(my_tesla.get_descriptive_name())
print(my_tesla.get_descriptive_name())
Import a Module in another
car.py
car.py Car
"""A
"""A set
set of
of classes
classes used
used to
to represent
represent gas
gas and
and electric
electric cars."""
cars."""
class
class Car:
Car:
--
-- same
same as
as before
before --
--

"""A
"""A set
set of
of classes
classes that
that can
can be
be used
used to
to represent
represent electric
electric cars."""
cars.""" electric_car.py
electric_car.py

from
from car
car import
import Car
Car
Battery
class
class Battery:
Battery:
--
-- same
same as
as before
before --
--
ElectricCar
class
class ElectricCar(Car):
ElectricCar(Car):
--
-- same
same as
as before
before --
--
Import a Module in another Car Battery

ElectricCar

from
from car
car import
import Car
Car
from
from electric_car
electric_car import
import ElectricCar
ElectricCar

my_beetle
my_beetle == Car('volkswagen',
Car('volkswagen', 'beetle',
'beetle', 2019)
2019)
print(my_beetle.get_descriptive_name())
print(my_beetle.get_descriptive_name())

my_tesla
my_tesla == ElectricCar('tesla',
ElectricCar('tesla', 'roadster',
'roadster', 2019)
2019)
print(my_tesla.get_descriptive_name())
print(my_tesla.get_descriptive_name()) my_cars.py
my_cars.py
Importing Multiple Modules Car Battery

ElectricCar

from
from car
car import
import Car
Car
from
from electric_car
electric_car import
import ElectricCar
ElectricCar as
as EC
EC

my_beetle
my_beetle == Car('volkswagen',
Car('volkswagen', 'beetle',
'beetle', 2019)
2019)
print(my_beetle.get_descriptive_name())
print(my_beetle.get_descriptive_name())

my_tesla
my_tesla == EC('tesla',
EC('tesla', 'roadster',
'roadster', 2019)
2019)
print(my_tesla.get_descriptive_name())
print(my_tesla.get_descriptive_name()) my_cars.py
my_cars.py
Inheritance, Importing Classes
Car Class
class Car:
class Car: car.py
def __init__(self, make, model, year): car.py
def __init__(self, make, model, year):
self.make = make
self.make = make
self.model = model
self.model = model
self.year = year
self.year = year
self.odometer_reading = 0
self.odometer_reading = 0

def get_descriptive_name(self):
def get_descriptive_name(self):
long_name = f"{self.year} {self.manufacturer} {self.model}"
long_name = f"{self.year} {self.manufacturer} {self.model}"
return long_name.title()
return long_name.title()

def read_odometer(self):
def read_odometer(self):
print(f"This car has {self.odometer_reading} miles on it.")
print(f"This car has {self.odometer_reading} miles on it.")

def update_odometer(self, mileage):


def update_odometer(self, mileage):
if mileage >= self.odometer_reading:
if mileage >= self.odometer_reading:
self.odometer_reading = mileage
self.odometer_reading = mileage
else:
else:
print("You can't roll back an odometer!")
print("You can't roll back an odometer!")

def increment_odometer(self, miles):


def increment_odometer(self, miles):
self.odometer_reading += miles
self.odometer_reading += miles
Importing Car Class
from
from car
car import
import Car
Car my_car.py
my_car.py

my_new_car
my_new_car == Car('audi',
Car('audi', 'a4',
'a4', 2019)
2019)
print(my_new_car.get_descriptive_name())
print(my_new_car.get_descriptive_name())
my_new_car.odometer_reading
my_new_car.odometer_reading == 23
23
my_new_car.read_odometer()
my_new_car.read_odometer()
Inheritance – Electric Car
class
class Car:
Car:

--
-- same
same as
as before
before --
--

class
class ElectricCar(Car):
ElectricCar(Car):
"""Represent
"""Represent aspects
aspects of
of aa car,
car, specific
specific to
to electric
electric vehicles."""
vehicles."""
Inheritance – Electric Car
class
class Car:
Car:

--
-- same
same as
as before
before --
--

class
class ElectricCar(Car):
ElectricCar(Car):
"""Represent
"""Represent aspects
aspects of
of aa car,
car, specific
specific to
to electric
electric vehicles."""
vehicles."""

def
def __init__(self,
__init__(self, make,
make, model,
model, year):
year):
"""Initialize
"""Initialize attributes of the parent
attributes of the parent class."""
class."""
super().__init__(make,
super().__init__(make, model,
model, year)
year)
Inheritance – Electric Car
class
class Car:
Car:

--
-- same
same as
as before
before --
--

class
class ElectricCar(Car):
ElectricCar(Car):
"""Represent
"""Represent aspects
aspects of
of aa car,
car, specific
specific to
to electric
electric vehicles."""
vehicles."""

def
def __init__(self,
__init__(self, make,
make, model,
model, year):
year):
"""Initialize
"""Initialize attributes of the parent
attributes of the parent class."""
class."""
super().__init__(make,
super().__init__(make, model,
model, year)
year)

super(
super() )calls
callsaamethod
methodfrom
fromthe
theparent
parentclass
class
Calling
Calling the __init__( ) method fromCar
the __init__( ) method from Car----gives
givesan
anElectricCar
ElectricCarinstance
instanceall
allthe
theattributes
attributes
defined
definedininparent’s
parent’s__init__(
__init__() )
Parent
Parentisisthe
thesuperclass
superclassand
andChild
Childclass
classisisthe
thesubclass
subclass
Inheritance – Electric Car
class
class Car:
Car:

--
-- same
same as
as before
before --
--

class
class ElectricCar(Car):
ElectricCar(Car):
"""Represent
"""Represent aspects
aspects of
of aa car,
car, specific
specific to
to electric
electric vehicles."""
vehicles."""

def
def __init__(self,
__init__(self, make,
make, model,
model, year):
year):
"""Initialize
"""Initialize attributes of the parent
attributes of the parent class."""
class."""
super().__init__(make,
super().__init__(make, model,
model, year)
year)

my_tesla
my_tesla == ElectricCar('tesla',
ElectricCar('tesla', 'model
'model s',
s', 2019)
2019)
print(my_tesla.get_descriptive_name())
print(my_tesla.get_descriptive_name())
Inheritance

When creating a specialized version of another class, use
inheritance

When one class inherits from another, it takes on the
attributes and methods of the first class.

Parent class, child class

Child class inherits any or all of the attributes and methods of
its parent class and defines new attributes and methods of its
own
Attributes and Methods for Child Class
class
class Car:
Car:

--
-- same
same as
as before
before --
--

class
class ElectricCar(Car):
ElectricCar(Car):
"""Represent
"""Represent aspects
aspects of
of aa car,
car, specific
specific to
to electric
electric vehicles."""
vehicles."""

def
def __init__(self,
__init__(self, make,
make, model,
model, year):
year):
"""Initialize
"""Initialize attributes of the parent
attributes of the parent class."""
class."""
super().__init__(make,
super().__init__(make, model,
model, year)
year)
Attributes and Methods for Child Class
class
class Car:
Car:

--
-- same
same as
as before
before --
--

class
class ElectricCar(Car):
ElectricCar(Car):
def
def __init__(self,
__init__(self, make,
make, model,
model, year):
year):
"""
"""
Initialize
Initialize attributes
attributes of
of the
the parent
parent class.
class.
Then
Then initialize
initialize attributes
attributes specific
specific to
to an
an electric
electric car.
car.
"""
"""
super().__init__(make,
super().__init__(make, model,
model, year)
year)
self.battery_size = 75
self.battery_size = 75
Attributes and Methods for Child Class
class
class Car:
Car:

--
-- same
same as
as before
before --
--

class
class ElectricCar(Car):
ElectricCar(Car):
def
def __init__(self,
__init__(self, make,
make, model,
model, year):
year):
"""
"""
Initialize
Initialize attributes
attributes of
of the
the parent
parent class.
class.
Then
Then initialize
initialize attributes
attributes specific
specific to
to an
an electric
electric car.
car.
"""
"""
super().__init__(make,
super().__init__(make, model,
model, year)
year)
self.battery_size = 75
self.battery_size = 75

def
def describe_battery(self):
describe_battery(self):
"""Print
"""Print aa statement
statement describing
describing the
the battery
battery size."""
size."""
print(f"This
print(f"This car
car has
has aa {self.battery_size}-kWh
{self.battery_size}-kWh battery."’
battery."’
Attributes and Methods for Child Class
class Car:
class Car:

-- same as before --
-- same as before --

class ElectricCar(Car):
class ElectricCar(Car):
def __init__(self, make, model, year):
def __init__(self, make, model, year):
"""
"""
Initialize
Initialize attributes
attributes of
of the
the parent
parent class.
class.
Then
Then initialize
initialize attributes
attributes specific
specific to
to an
an electric
electric car.
car.
"""
"""
super().__init__(make,
super().__init__(make, model,
model, year)
year)
self.battery_size = 75
self.battery_size = 75

def describe_battery(self):
def describe_battery(self):
"""Print a statement describing the battery size."""
"""Print a statement describing the battery size."""
print(f"This car has a {self.battery_size}-kWh battery."
print(f"This car has a {self.battery_size}-kWh battery."

my_tesla
my_tesla == ElectricCar('tesla',
ElectricCar('tesla', 'model
'model s',
s', 2019)
2019)
print(my_tesla.get_descriptive_name())
print(my_tesla.get_descriptive_name())
my_tesla.describe_battery()name())
my_tesla.describe_battery()name())
Overriding methods from Parent Class
class
class Car:
Car:

--
-- same
same as
as before
before --
--

class
class ElectricCar(Car):
ElectricCar(Car):
--
-- same
same as
as before
before --
--

def
def fill_gas_tank(self):
fill_gas_tank(self):
"""Electric
"""Electric cars
cars don't
don't have
have gas
gas tanks."""
tanks."""
print("This
print("This car
car doesn't
doesn't need
need aa gas
gas tank!")
tank!")
Overriding methods from Parent Class

Say the parent class had a class
class Car:
Car:

method fill_gas_tank() --
-- same
same as
as before
before --
--


Defining a method of the same class
class ElectricCar(Car):
ElectricCar(Car):
name in the child class will --
-- same as
same as before
before --
--

override the behaviour of the def


def fill_gas_tank(self):
fill_gas_tank(self):
parent’s method """Electric
"""Electric cars
print("This
cars don't
don't have
have gas
gas tanks."""
tanks."""
print("This car
car doesn't
doesn't need
need aa gas
gas tank!")
tank!")
Overriding methods from Parent Class

Say the parent class had a method class
class Car:
Car:

fill_gas_tank() --
-- same
same as
as before
before --
--

Defining a method of the same class
class ElectricCar(Car):
ElectricCar(Car):
name in the child class will override --
-- same as
same as before
before --
--
the behaviour of the parent’s method
def
def fill_gas_tank(self):
fill_gas_tank(self):

Calling the fill_gas_tank() method in """Electric
"""Electric cars
cars don't
don't have
have gas
gas tanks."""
tanks."""
an ElectricCar object will run this print("This
print("This car
car doesn't
doesn't need
need aa gas
gas tank!")
tank!")

code

Inheritance: child class retains what
is needed and overrides everything
else from the parent class.
Instances as Attributes
class Car:
class Car:
-- same as before --
-- same as before --

class Battery:
class Battery:
"""A simple attempt to model a battery for an electric car."""
"""A simple attempt to model a battery for an electric car."""
def __init__(self, battery_size=75):
def __init__(self, battery_size=75):
"""Initialize the battery's attributes."""
"""Initialize the battery's attributes."""
self.battery_size = battery_size
self.battery_size = battery_size
def describe_battery(self):
def describe_battery(self):
"""Print a statement describing the battery size."""
"""Print a statement describing the battery size."""
print(f"This car has a {self.battery_size}-kWh battery")
print(f"This car has a {self.battery_size}-kWh battery")

class ElectricCar(Car):
class ElectricCar(Car):
"""Represent aspects of a car, specific to electric vehicles."""
"""Represent aspects of a car, specific to electric vehicles."""
def __init__(self, make, model, year):
def __init__(self, make, model, year):
"""
"""
Initialize attributes of the parent class.
Initialize attributes of the parent class.
Then initialize attributes specific to an electric car.
Then initialize attributes specific to an electric car.
"""
"""
super().__init__(make, model, year)
super().__init__(make, model, year)
self.battery = Battery()
self.battery = Battery()
Instances as Attributes
class Car:
class Car:
-- same as before --
-- same as before --

class Battery:
class Battery:
"""A simple attempt to model a battery for an electric car."""
"""A simple attempt to model a battery for an electric car."""
def __init__(self, battery_size=75):
def __init__(self, battery_size=75):
"""Initialize the battery's attributes."""
"""Initialize the battery's attributes."""
self.battery_size = battery_size
self.battery_size = battery_size
def describe_battery(self):
def describe_battery(self):
"""Print a statement describing the battery size."""
"""Print a statement describing the battery size."""
print(f"This car has a {self.battery_size}-kWh battery")
print(f"This car has a {self.battery_size}-kWh battery")

class ElectricCar(Car):
class ElectricCar(Car):
"""Represent aspects of a car, specific to electric vehicles."""
"""Represent aspects of a car, specific to electric vehicles."""
def __init__(self, make, model, year):
def __init__(self, make, model, year):
"""
"""
Initialize attributes of the parent class.
Initialize attributes of the parent class.
Then initialize attributes specific to an electric car.
Then initialize attributes specific to an electric car.
"""
"""
super().__init__(make, model, year)
super().__init__(make, model, year) my_tesla
my_tesla == ElectricCar('tesla',
ElectricCar('tesla', 'model
'model s',
s', 2019)
2019)
self.battery = Battery()
self.battery = Battery() print(my_tesla.get_descriptive_name())
print(my_tesla.get_descriptive_name())
my_tesla.battery.describe_battery()
my_tesla.battery.describe_battery()
class
class Car:
--
Car:
-- same as
same as before
before --
--
Instances as Attributes
class
class Battery:
Battery:
--
-- same
same as
as before
before --
--
def
def get_range(self):
get_range(self):
"""Print
"""Print aa statement
statement about
about the
the range
range this
this battery
battery provides."""
provides."""
if
if self.battery_size
self.battery_size ==
== 75:
75:
range = 260
range = 260
elif
elif self.battery_size
self.battery_size ==
== 100:
100:
range = 315
range = 315

print(f"This
print(f"This car
car can
can go
go about
about {range}
{range} miles
miles on
on aa full
full charge.")
charge.")

class
class ElectricCar(Car):
ElectricCar(Car):
--
-- same as
same as before
before --
--
class
class Car:
--
Car:
-- same as
same as before
before --
--
Instances as Attributes
class
class Battery:
Battery:
--
-- same
same as
as before
before --
--
def
def get_range(self):
get_range(self):
"""Print
"""Print aa statement
statement about
about the
the range
range this
this battery
battery provides."""
provides."""
if
if self.battery_size
self.battery_size ==
== 75:
75:
range = 260
range = 260
elif
elif self.battery_size
self.battery_size ==
== 100:
100:
range = 315
range = 315

print(f"This
print(f"This car
car can
can go
go about
about {range}
{range} miles
miles on
on aa full
full charge.")
charge.")

class my_tesla
my_tesla == ElectricCar('tesla',
class ElectricCar(Car):
ElectricCar(Car): ElectricCar('tesla', 'model
'model s',
s', 2019)
2019)
-- same as before --
-- same as beforeprint(my_tesla.get_descriptive_name())
--
print(my_tesla.get_descriptive_name())
my_tesla.battery.describe_battery()
my_tesla.battery.describe_battery()
my_tesla.battery.get_range()
my_tesla.battery.get_range()
Parent and Subclass in the same file
class
class Car:
Car: Car
-- car.py
car.py
-- same
same as
as before
before --
--

class
class ElectricCar(Car):
ElectricCar(Car): ElectricCar
--
-- same
same as
as before
before --
--
Importing subclass car.py
car.py
"""A
"""A set
set of
of classes
classes used
used to
to represent
represent gas
gas and
and electric
electric cars."""
cars.""" Car
class Car:
class Car:
--
-- same
same as
as before
before --
--
ElectricCar

class
class ElectricCar(Car):
ElectricCar(Car):
--
-- same as
same as before
before --
--

my_electric_cars.py
my_electric_cars.py
from
from car
car import
import ElectricCar
ElectricCar

my_tesla
my_tesla == ElectricCar('tesla',
ElectricCar('tesla', 'model
'model s',
s', 2019)
2019)
print(my_tesla.get_descriptive_name())
print(my_tesla.get_descriptive_name())
my_tesla.battery.describe_battery()
my_tesla.battery.describe_battery()
my_tesla.battery.get_range()
my_tesla.battery.get_range()
Importing Multiple Subclasses car.py
car.py
"""A
"""A set
set of
of classes
classes used
used to
to represent
represent gas
gas and
and electric
electric cars."""
cars.""" Car
class Car:
class Car:
--
-- same
same as
as before
before --
-- ElectricCar

class
class ElectricCar(Car):
ElectricCar(Car):
--
-- same as
same as before
before --
--

my_cars.py
my_cars.py
from
from car
car import
import Car,
Car, ElectricCar
ElectricCar

my_beetle
my_beetle == Car('volkswagen',
Car('volkswagen', 'beetle',
'beetle', 2019)
2019)
print(my_beetle.get_descriptive_name())
print(my_beetle.get_descriptive_name())

my_tesla
my_tesla == ElectricCar('tesla',
ElectricCar('tesla', 'roadster',
'roadster', 2019)
2019)
print(my_tesla.get_descriptive_name())
print(my_tesla.get_descriptive_name())
Importing Entire Module car.py
car.py
"""A
"""A set
set of
of classes
classes used
used to
to represent
represent gas
gas and
and electric
electric cars."""
cars.""" Car
class Car:
class Car:
--
-- same
same as
as before
before --
-- ElectricCar

class
class ElectricCar(Car):
ElectricCar(Car):
--
-- same as
same as before
before --
--

my_cars.py
my_cars.py
import
import car
car

my_beetle
my_beetle == car.Car('volkswagen',
car.Car('volkswagen', 'beetle',
'beetle', 2019)
2019)
print(my_beetle.get_descriptive_name())
print(my_beetle.get_descriptive_name())

my_tesla
my_tesla == car.ElectricCar('tesla',
car.ElectricCar('tesla', 'roadster',
'roadster', 2019)
2019)
print(my_tesla.get_descriptive_name())
print(my_tesla.get_descriptive_name())
Import a Module in another
car.py
car.py Car
"""A
"""A set
set of
of classes
classes used
used to
to represent
represent gas
gas and
and electric
electric cars."""
cars."""
class Car:
class Car:
--
-- same
same as
as before
before --
--

"""A
"""A set
set of
of classes
classes that
that can
can be
be used
used to
to represent
represent electric
electric cars."""
cars.""" electric_car.py
electric_car.py

from
from car
car import
import Car
Car
Battery
class
class Battery:
Battery:
--
-- same as
same as before
before --
--
ElectricCar
class
class ElectricCar(Car):
ElectricCar(Car):
--
-- same as
same as before
before --
--
Import a Module in another Car Battery

ElectricCar

from
from car
car import
import Car
Car
from
from electric_car import
electric_car import ElectricCar
ElectricCar

my_beetle
my_beetle == Car('volkswagen',
Car('volkswagen', 'beetle',
'beetle', 2019)
2019)
print(my_beetle.get_descriptive_name())
print(my_beetle.get_descriptive_name())

my_tesla
my_tesla == ElectricCar('tesla',
ElectricCar('tesla', 'roadster',
'roadster', 2019)
2019)
print(my_tesla.get_descriptive_name())
print(my_tesla.get_descriptive_name()) my_cars.py
my_cars.py
Importing Multiple Modules Car Battery

ElectricCar

from
from car
car import
import Car
Car
from
from electric_car import
electric_car import ElectricCar
ElectricCar as
as EC
EC

my_beetle
my_beetle == Car('volkswagen',
Car('volkswagen', 'beetle',
'beetle', 2019)
2019)
print(my_beetle.get_descriptive_name())
print(my_beetle.get_descriptive_name())

my_tesla
my_tesla == EC('tesla',
EC('tesla', 'roadster',
'roadster', 2019)
2019)
print(my_tesla.get_descriptive_name())
print(my_tesla.get_descriptive_name()) my_cars.py
my_cars.py

You might also like