Introduction to duck typing and polymorphism in Python

    Duck typing is a unique way of programming in Python, referring to the concept of a dynamic type (as long as an animal walks like a duck and quacks like a duck, then it can be used as a duck).

This means that in Python, you can use multiple different class objects to perform the same operation, as long as they have the same methods and attributes.

    Polymorphism is one of the three major features of object-oriented programming languages ​​(encapsulation, inheritance, polymorphism), which refers to the same behavior through different objects. Languages ​​with polymorphic features can apply the same method to different objects, so that different subclasses can implement the same interface to handle different behaviors.

For example, in Python, the animal class Animal has a say() method, and the dog subclass Dog and the cat subclass Cat override the say() method respectively, and can use the same method name to call it to express different Behavior.

The specific code is as follows:

class Animal:
    def say(self):
        print("我是一只动物,我会叫")




class Cat(Animal):
    def say(self):
        print("我是一只猫,我是这样叫的:喵喵喵")




class Dog(Animal):
    def say(self):
        print("我是一只猫,我是这样叫的:汪汪汪")




animal = Cat()
animal.say()  # 喵喵喵


animal = Dog()
animal.say()  # 汪汪汪

operation result:

2a206b1f49bd356f7928265cae7ac820.png

    The above code shows how to use polymorphism to achieve the effect of " the same method produces different results in different classes ". In this example, we define an Animal class that contains a say() method. We then created two subclasses Cat and Dog, both of which override the say() method in the base class.

In the test code, we first initialize the animal object as an instance of the Cat class. We then call the instance's say() method, which actually calls the overridden say() method in the Cat class, and prints out "I'm a cat and my name is: meow meow". Then, we reset the animal object to an instance of the Dog class, and call its say() method again, this time actually calling the overridden say() method in the Dog class, and print out "I am a A dog, I call it like this: woof woof woof".

This example illustrates that even if a method is defined in a base class, derived classes can override that method and implement their own logic. This is a very basic and important principle for implementing polymorphism . Through the polymorphic mechanism, we can write more flexible and easy-to-extend programs.

Next, let’s take a look at the use of classes as variables. This is something I haven’t paid much attention to before:

class Cat(object):
    def say(self):
        print("我是一只猫,喵喵喵")




class Dog(object):
    def say(self):
        print("我是一只猫,汪汪汪")




animal_list = [Cat, Dog] 


for animal in animal_list:
    animal().say()

In the above code, Cat and Dog in line 11 will be treated as variables in the list, and only after they are instantiated in line 14 will it be clear that Cat and Dog are certain classes.

 Recently, I plan to set up a variety of check-in learning. Interested friends can chat privately on WeChat (xiaobotester) to study together.

Guess you like

Origin blog.csdn.net/liboshi123/article/details/129891601