-
Integers, Floats and Strings 3
-
Lecture1.1
-
Lecture1.2
-
Lecture1.3
-
-
If Statements 3
-
Lecture2.1
-
Lecture2.2
-
Lecture2.3
-
-
Lists, Sets and Tuples 5
-
Lecture3.1
-
Lecture3.2
-
Lecture3.3
-
Lecture3.4
-
Lecture3.5
-
-
Loops 3
-
Lecture4.1
-
Lecture4.2
-
Lecture4.3
-
-
Functions 3
-
Lecture5.1
-
Lecture5.2
-
Lecture5.3
-
-
Dictionaries 3
-
Lecture6.1
-
Lecture6.2
-
Lecture6.3
-
-
Assertions 2
-
Lecture7.1
-
Lecture7.2
-
-
Classes 3
-
Lecture8.1
-
Lecture8.2
-
Lecture8.3
-
-
Matplotlib 2
-
Lecture9.1
-
Lecture9.2
-
Inheritance
The __str__ Function¶
The purpose of the __str is to define what happens when you print a class instance. Right now it is nothing useful, but when you define the __str you can decide what it should be. The way you do this is add it into the class declaration then return whatever should be outputted. Below, we make the function return the name.
class Person:
def __init__(self, age, name):
assert type(age) == int, "Age must be an integer"
assert type(name) == str, "Name must be a string"
self.age = age
self.name = name
def say_hi(self):
print("Hi")
def tell_age(self):
print("I am {} years old.".format(self.age))
def set_age(self, age):
assert type(age) == int, "Age must be an integer"
self.age = age
def __str__(self):
return self.name
#Create the class instance
carl = Person(25, "Carl")
#Print it
print(carl)
Carl
Sub-Classes and Inheritance¶
One way to extend a class is to create sub-classes. These classes inherit all the functionality of the class they were created off of while allowing for extensions of functionality. We define it by modifying the first line of the class definition to include the class to inherit from in parentheses. Below we create a class Student which is a sub-class of the Person class. Then we can make a function which takes as an argument college and returns a phrase that the person says including their name.
#Create a sub-class
class Student(Person):
def college_talk(self, college):
print("I'm {} and I go to {}".format(self.name, college))
When we create the instance of the student, we also are going to need to still pass in the same initialization arguments.
#Create the instance and then run the sub-class function
joe = Student(22, "Joe Smith")
joe.college_talk("Harvard")
I'm Joe Smith and I go to Harvard
Of course we also can use any of the functions that it had inherited too.
joe.tell_age()
I am 22 years old.
Adding More Initialization¶
In the case that we want to add specific initialization to our sub-class, we can do the following:
- Create the __init__ function to accept the arguments for the super class as well as any new arguments.
- Call super().__init__() and pass in any arguments needed for the super class.
- Do any other definitions as needed for the sub-class.
Below we do just that by adding a college argument and then modifying the function from before to report back that college we set the student up with.
class Student(Person):
def __init__(self, age, name, college):
super().__init__(age, name)
self.college = college
def college_talk(self):
print("I'm {} and I go to {}".format(self.name, self.college))
joe = Student(22, "Joe Smith", "Harvard")
joe.college_talk()
I'm Joe Smith and I go to Harvard