Skip to content

Instantly share code, notes, and snippets.

@kipkitur
Created July 16, 2020 02:44
Show Gist options
  • Save kipkitur/bc2de205dca8f283d62ba8727cbe7d6f to your computer and use it in GitHub Desktop.
Save kipkitur/bc2de205dca8f283d62ba8727cbe7d6f to your computer and use it in GitHub Desktop.
#parent class 1
class Item():
def __init__(self, sku):
self.sku = sku
#create a parent class method
#create an example method for this class to show how the child class accesses the attributes and methods from the parent class
def print_sku(self):
print("The sku is {}".format(self.sku))
#parent class 2
class Garment():
def __init__(self, section, type):
self.section = section
self.type = type
#create a parent class method
#create an example method for this class to show how the child class accesses the attributes and methods from the parent class
def print_garment(self):
print("This garment is in section {}, and is a {}".format(self.section,self.type))
#child class which will inherit from the all the parents
class Shirts(Item, Garment):
#identify attributes from the parent class and those that are specific to the child class
def __init__(self, sku, section, type, name, color):
#first define attributes specific to the child class i.e. name & color
self.name = name
self.color = color
#initialize attributes from parent class 1
Item.__init__(self, sku)
#initialize attributes from parent class 2
Garment.__init__(self, section, type)
#create a child class method
def print_shirt(self):
print("It is {} and is suitable for {} events. Grab it while it is on sale".format(self.color,self.name))
#create a child class item
Polo = Shirts(20345, 12, "Top", "Sport", "Red")
#call each of the parent methods AND the child method
Polo.print_sku()
Polo.print_garment()
Polo.print_shirt()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment