Python 3home |
Introduction to Python
davidbpython.com
Child classes inherit attributes from Parent classes.
class Parent:
parv = 500
class Child(Parent): # Child inherits from Parent
chi_val = 1
def __init__(self, iv):
self.iv = iv
c = Child(88)
print(c.iv) # 88 (retrieved from instance)
print(c.chi_val) # 1 (retrieved from the Child class)
print(c.parv) # 500 (retrieved from the Parent class)
General behaviors can go in the parent, and more specific in the children.
class Animal:
def __init__(self, name):
self.name = name
def eat(self, food):
print(f'{self.name} eats the {food}.')
class Dog(Animal):
def fetch(self, thing):
print('{self.name} brings back the {thing}!')
class Cat(Animal):
pass # means empty block
d = Dog('Rover')
c = Cat('Fluffly')
d.eat('dog food') # Rover eats the dog food.
c.eat('cat food') # Flffy eats the cat food.
d.fetch('stick') # Rover brings back the stick!
Child class behavior can build upon the general behavior in the parent.
class Animal:
def __init__(self, name):
self.name = name
def eat(self, food):
print(f'{self.name} eats {food}.')
class Dog(Animal):
pass
class Cat(Animal):
def eat(self, food):
if food != 'sushi':
print('snif - snif - nah!')
else:
super().eat(food) # calls Animal.eat()
d = Dog('Rover')
c = Cat('Fluffly')
d.eat('dog food') # Rover eats the dog food.
c.eat('cat food') # snif - snif - nah!
c.eat('sushi') # Fluffy eats the sushi.
Same-named methods in two different classes can share a conceptual similarity.
class Animal:
def __init__(self, name):
self.name = name
class Dog(Animal):
def speak(self):
print(f'{self.name}: Bark! Bark!')
class Cat(Animal):
def speak(self):
print(f'{self.name}: Meow!')
pet_list = [Dog('Rover'), Cat('Fluffy'), Cat('Precious'), Dog('Rex')]
for pet in pet_list:
pet.speak()
# Rover: Bark! Bark!
# Fluffy: Meow!
# Precious: Meow!
# Rex: Bark! Bark!
These are OOP terms we introduced in this session.
| class variable | See "class attribute". |
| class attribute | A value stored in the class, often defined as a variable within the class block. Class attributes are also accessible to its instances. |
| attribute lookup | Anytime an attribute of an instance or a class is accessed, Python must go through a process of trying to find it, as the attribute may be found in the instance, in the class, or in any classes from which its class inherits. |
| class method | A method that is designed to work with a class' attributes, rather than an instance's attributes. Contrast with instance method. |
| static method | A method that is not designed to work with either class or instance attributes, but belongs with the class because its functionality is related to what the class does. |
| instance method | A method that is designed to work with an instance's attributes. Also known as an object method. |
| object method | See instance method. |
| inheritance | The dynamic by which a "child" class, and its instances, are able to access attributes of a "parent" class. |
| polymorphism | The technique of creating two methods, of two different classes, that do different but conceptually similar things, and naming them the same name. Polymorphic methods can be called on either instance, with the expectation that the functionality appropriate to the instance of either type will result. |