Lecture11 Class Inheritance Iterators Polymorphism
Lecture11 Class Inheritance Iterators Polymorphism
Hyuntae Cho
Dept. of Digital Content
Tongmyong University
Python Classes/Objects
• Python is an object oriented programming language.
2
Create a Class
• To create a class, use the keyword class:
• Example
– Create a class named MyClass, with a property named x:
• Create Object
• Now we can use the class named MyClass to create objects:
• Example
– Create an object named p1, and print the value of x:
3
The __init__() Function
• The examples above are classes and objects in their simplest form,
and are not really useful in real life applications.
4
The __init__() Function
• Example
• Create a class named Person, use the __init__() function to assign
values for name and age:
Note: The __init__() function is called automatically every time the class is being used to create a new object.
5
The __str__() Function
• The __str__() function controls what should be returned when the
class object is represented as a string.
• Example
• The string representation of an object WITHOUT the __str__()
function:
6
The __str__() Function
• Example
• The string representation of an object WITH the __str__() function:
7
Object Methods
• Objects can also contain methods. Methods in objects are functions
that belong to the object.
• Example
– Insert a function that prints a greeting, and execute it on the p1 object:
• It does not have to be named self , you can call it whatever you like,
but it has to be the first parameter of any function in the class:
• Example
– Use the words mysillyobject and abc instead of self:
9
Modify Object Properties
• You can modify properties on objects like this:
• Example
– Set the age of p1 to 40:
– Practice:
10
Delete Object Properties
• You can delete properties on objects by using the del keyword:
• Example
– Delete the age property from the p1 object:
– practice
11
Delete Objects
• You can delete objects by using the del keyword:
• Example
– Delete the p1 object:
– practice
12
Python Programming
(Inheritance)
Hyuntae Cho
Dept. of Digital Content
Tongmyong University
Python Inheritance
• Parent class is the class being inherited from, also called base class.
• Child class is the class that inherits from another class, also called
derived class.
14
Create a Parent Class
• Any class can be a parent class, so the syntax is the same as creating
any other class:
• Example
– Create a class named Person, with firstname and lastname properties, and a
printname method:
15
Create a Child Class
• To create a class that inherits the functionality from another class,
send the parent class as a parameter when creating the child class:
• Example
– Create a class named Student, which will inherit the properties and methods
from the Person class:
Note: Use the pass keyword when you do not want to add any other properties or methods to the class.
• Now the Student class has the same properties and methods as the
Person class.
• Example
– Use the Student class to create an object, and then execute the printname
method:
16
Create a Child Class
• Total codes:
Inheritance
17
Add the __init__() Function
• So far we have created a child class that inherits the properties and
methods from its parent.
Note: The __init__() function is called automatically every time the class is being used to create a new object.
• Example
– Add the __init__() function to the Student class:
18
Add the __init__() Function
• When you add the __init__() function, the child class will no
longer inherit the parent's __init__() function.
Full code in
next page
19
Add the __init__() Function
Now we have successfully added the __init__() function, and kept the inheritance of the parent
class, and we are ready to add functionality in the __init__() function.
20
Use the super() Function
• Python also has a super() function that will make the child class
inherit all the methods and properties from its parent:
Full code
By using the super() function, you do not have to use the name of the parent element, it will
automatically inherit the methods and properties from its parent.
21
Add Properties
• Example 1
– Add a property called graduationyear to the Student class:
Full code
22
Add Properties
• Example 2
– Add a year parameter, and pass the correct year when creating objects:
Full code
23
Add Methods
• Example
– Add a method called welcome to the Student class:
24
Add Methods
• Full code
25
Practice
26
Python Programming
(Iterator)
Hyuntae Cho
Dept. of Digital Content
Tongmyong University
Python Iterators
• An iterator is an object that contains a countable number of values.
28
Iterator vs Iterable
• Lists, tuples, dictionaries, and sets are all iterable objects. They are
iterable containers which you can get an iterator from.
• Example
– Return an iterator from a tuple, and print each value:
29
Iterator vs Iterable
• Even strings are iterable objects, and can return an iterator:
• Example
– Strings are also iterable objects, containing a sequence of characters:
30
Looping Through an Iterator
• We can also use a for loop to iterate through an iterable object:
• Example
– Iterate the values of a tuple:
• Example
– Iterate the characters of a string:
31
Create an Iterator
32
Create an Iterator
• Example
– Create an iterator that returns numbers, starting with 1, and each sequence
will increase by one (returning 1,2,3,4,5 etc.):
33
StopIteration
• The example above would continue forever if you had enough next()
statements, or if it was used in a for loop.
34
StopIteration
• Example
• Stop after 20 iterations:
35
Python Programming
(Polymorphism)
Hyuntae Cho
Dept. of Digital Content
Tongmyong University
Python Polymorphism
• The word "polymorphism" means "many forms", and in
programming it refers to methods/functions/operators with the same
name that can be executed on many objects or classes.
• Function Polymorphism
– An example of a Python function that can be used on different objects is the
len() function.
37
Function Polymorphism
• String
– For strings len() returns the number of characters:
• Tuple
– For tuples len() returns the number of items in the tuple:
• Dictionary
– For dictionaries len() returns the number of key/value pairs in the dictionary:
38
Class Polymorphism
• Polymorphism is often used in Class methods, where we can have
multiple classes with the same method name.
• For example, say we have three classes: Car, Boat, and Plane, and
they all have a method called move():
39
Look at the for loop at the end. Because of
polymorphism we can execute the same
Class Polymorphism method for all three classes.
40
Inheritance Class Polymorphism
• What about classes with child classes with the same name? Can we
use polymorphism there?
• Yes. If we use the example above and make a parent class called
Vehicle, and make Car, Boat, Plane child classes of Vehicle, the child
classes inherits the Vehicle methods, but can override them:
41
Inheritance Class Polymorphism
• Example
– Create a class called Vehicle and make Car, Boat, Plane child classes of
Vehicle:
42
Inheritance Class Polymorphism
• Child classes inherits the properties and methods from the parent
class.
• In the example above you can see that the Car class is empty, but it
inherits brand, model, and move() from Vehicle.
• The Boat and Plane classes also inherit brand, model, and move()
from Vehicle, but they both override the move() method.
43
Conclusion
44