The art and Tao of Python Programming: an introduction to Python language
Link to the video course "the art and Tao of Python Programming: an introduction to Python language": https://edu.csdn.net/course/detail/27845
Polymorphism
The word polymorphism means having many forms. In programming, polymorphism means that the same function name (but different signatures) can be used for different types.
Function signature: contains the information of a function, including function name, parameter type, number of parameters, order, and its class and namespace. Function signature is used to identify different functions. The name of the function is only part of the function signature. For functions with different function signatures, even if the function names are the same, the interpreter (compilers and linkers in other languages) considers them to be different functions.
Examples of Python built-in polymorphic functions:
# Python program to demonstrate in-built poly- # morphic functions # len() being used for a string print(len("course")) # len() being used for a list print(len([10, 20, 30]))
6 3
Examples of user-defined polymorphic functions:
# A simple Python function to demonstrate # Polymorphism def add(x, y, z = 0): return x + y + z # Driver code print(add(2, 3)) print(add(2, 3, 4))
5 9
Polymorphism of class methods:
class China(): def capital(self): print("Beijing is the capital of China.") def language(self): print("Mandarin is the primary language of China.") def type(self): print("China is a developing country.") class USA(): def capital(self): print("Washington, D.C. is the capital of USA.") def language(self): print("English is the primary language of USA.") def type(self): print("USA is a developed country.") obj_china = China() obj_usa = USA() for country in (obj_china, obj_usa): country.capital() country.language() country.type()
Beijing is the capital of China. Mandarin is the primary language of China. China is a developing country. Washington, D.C. is the capital of USA. English is the primary language of USA. USA is a developed country.
Polymorphism during class inheritance:
In Python, through polymorphism, you can define a method with the same name as the method in the parent class in a subclass. In inheritance, a subclass inherits methods from its parent class. However, you can modify methods in subclasses that inherit from the parent class. This is particularly useful when methods inherited from the parent class are not suitable for subclasses. In this case, we will re implement the method in the subclass. The process of re implementing a method in a subclass is called "method rewriting".
class Bird: def intro(self): print("There are many types of birds.") def flight(self): print("Most of the birds can fly but some cannot.") class sparrow(Bird): def flight(self): print("Sparrows can fly.") class ostrich(Bird): def flight(self): print("Ostriches cannot fly.") obj_bird = Bird() obj_spr = sparrow() obj_ost = ostrich() obj_bird.intro() obj_bird.flight() obj_spr.intro() obj_spr.flight() obj_ost.intro() obj_ost.flight()
There are many types of birds. Most of the birds can fly but some cannot. There are many types of birds. Sparrows can fly. There are many types of birds. Ostriches cannot fly.
Polymorphism of functions and objects:
You can also create a function that can accept any object to achieve polymorphism. In this example, we create a function called func (), which will use an object called obj. Although we use the obj name, any instantiated object can be called in this function. Next, let the function do something with the obj object passed to it. In this case, let's call three methods, namely capital(), language() and type(), which are defined in China and USA classes respectively. Next, let's create instantiations of China and USA classes. With these, we can use the same func() function to call their actions:
Use functions to achieve polymorphism:
class China(): def capital(self): print("Beijing is the capital of China.") def language(self): print("Mandarin is the primary language of China.") def type(self): print("China is a developing country.") class USA(): def capital(self): print("Washington, D.C. is the capital of USA.") def language(self): print("English is the primary language of USA.") def type(self): print("USA is a developed country.") def func(obj): obj.capital() obj.language() obj.type() obj_china = China() obj_usa = USA() func(obj_china) func(obj_usa)
Beijing is the capital of China. Mandarin is the primary language of China. China is a developing country. Washington, D.C. is the capital of USA. English is the primary language of USA. USA is a developed country.