I. What is polymorphism?Polymorphism relies on the inheritance and derivation of classes, which means that functions with different functions can use the same function names, so that functions with different contents can be called with a function name.For a better understanding, take the Len () function to give an example:The function of the Len () function is to find the length of a sequence, so how does t
Python polymorphism instance analysis, python Polymorphism
This example describes the Python polymorphism. Share it with you for your reference. The details are as follows:
#! /Usr/bin/env p
I often talk about duck and Polymorphism in python, and I often talk about python polymorphism.
1. What is polymorphism?
2. polymorphism in python
Iii. Dynamic Language and duck type
Python object-oriented 2: inheritance, polymorphism, python Polymorphism
The second feature of object-oriented is inheritance.
The methods of multiple classes can be extracted to the parent class, And the subclass only needs to inherit the parent class;
The basic syntax is the new class name of the class (parent class
, that is, Python itself is to support polymorphism, so what is the advantage of doing so?(1) Increased flexibility of the programStatus quo, regardless of the ever-changing object, the user is the same form to invoke, such as func (animal)(2) Increased scalability of the programBy inheriting the animal class, a new class is created, and the user does not have to change their own code or use Func (animal) t
animals there must be a talk method # So we can not consider the specific type of the three of them, and directly using C.drink () d.drink () P.drink()# Further, we can define a unified interface to use def func (obj): obj.talk ()two why to use polymorphism (the benefit of polymorphism)In fact, we can see from the above polymorphism example, we have not ad
polymorphism and dynamic polymorphism.Static polymorphism: If any type can be operated with operator +Dynamic polymorphism is as follows:Peo=people () Dog=dog () Pig=pig () #peo, dog, pig are animals, as long as the animal must have talk method # so we can not consider the specific type of the three, and directly use Peo.talk () Dog.talk () Pig.talk () #更进一步, we
polymorphism: sending the same message to different objects (!!!) Obj.func (): Is the method func that called obj, also known as a message Func sent to obj, and different objects have different behavior (that is, methods) when they are received. In other words, each object can respond to a common message in its own way. The so-called message, is called the function, the different behavior refers to the different implementation, namely executes the di
Referenced by: http://www.cnblogs.com/linhaifeng/articles/7340687.htmlPolymorphism in Python refers to a class of things that have many forms. For example, animals have many forms, people, dogs, cats, and so on.ImportABCclassAnimal (METACLASS=ABC. Abcmeta):#The same kind of things: animals@abc. AbstractmethoddefTalk (self):PassclassPeople (Animal):#one of the forms of animals: Man defTalk (self):Print('Say hello')classDog (Animal):#Animal Form Two:
First, review:1, the process: The core is the process, the process is the steps to solve the problem, that is, what to do first, what to do, based on process-oriented design procedures, like in the design of a pipeline, is a mechanical thinking method.Pros: Complex questions to simplifyCons: Poor scalability (holding a body)Application scenario: Linux kernel, httpd,git2, Object-oriented programming: The core is the object, to understand the object should regard themselves as God, in the eyes of
Python Object-Oriented Programming inheritance and polymorphism explanation, python Object-Oriented Programming
This article describes the inheritance and Polymorphism of Python object-oriented programming. We will share this with you for your reference. The details are as f
the same, execution of the result is not the same as func (peo1) func ( PIG1) func (D1)(2).Why use polymorphism (the benefit of polymorphism)In fact, we can see from the above polymorphism example, we do not add the above new knowledge, that is, Python itself is to support polymor
Python 31st days ----- class encapsulation, inheritance, polymorphism..., python 31st days
Encapsulation
Encapsulation is better understood. Encapsulation is one of the characteristics of object-oriented, and is the main feature of object and class concepts.
Encapsulation refers to encapsulating objective objects into abstract classes. classes can only perform tr
Polymorphism is a basic feature of Object-Oriented Language. polymorphism means that variables do not know what the referenced object is, and different behavior methods are presented based on different referenced objects, the following uses an example to learn how to use it. when dealing with multi-state objects, you only need to pay attention to its interface. in pytho
In the previous article, we talked about the basic definition and usage of classes, which only embodies one of the three characteristics of object-oriented programming: encapsulation. Let's look at two other features: inheritance and polymorphism.
In Python, you can have a class inherit a class, which is called a parent class or superclass, or it can be called a base class, and the inherited class is called
does not change. Iv. binding methods and non-binding methods1, the functions defined in the class are divided into two categories, one is a function with a binding method, and one is a function without a binding method.2. Functions with binding methods2.1 Methods that are bound to an object: A function property defined in a class is bound to an object by default, who is bound to it, and is automatically called by the bound person asThe first parameter is passed in.2.2 Methods that are bound to
invoke also will be the class as the first parameter passed), Python has built a function Classmethod for us to define the functions in the class as class methodshost='127.0.0.1'PORT=3306db_path=r'C:\Users \administrator\pycharmprojects\test\ Object-Oriented programming \test1\db'settings.py ContentImport Settingsimport hashlibimport timeclass MySQL: def __init__ (self,host,port): self.host=host Self.port=port @classmethod def
to point to the Child class object. In python, each object has the same object header-pyobejct content, which makes references to objects in Python very uniform, you only need to use a pyobject * pointer to reference any object. This is necessary to implement polymorphism.
// XK> how to represent the type
The object type defines the data members of the object an
Learning to polymorphic, not sure whether they fully understand the Python polymorphism, so Google watched a lot of information and literature, found that many programmers speak too abstract, not easy for beginners to understand, this violates the original intention of Python: simple. So I made a summary here in a popular way. First put a piece of code, in the Py
data types we define are the same data types that python comes with, such as STR, list, dict:a = list() # a是list类型b = Animal() # b是Animal类型c = Dog() # c是Dog类型Judging whether a variable is a type can be isinstance() judged by:>>> isinstance(a, list)True>>> isinstance(b, Animal)True>>> isinstance(c, Dog)TrueIt seems that a, B and C do correspond to the list, Animal, dog 3 types.But wait, try it:>>> isinstance(c, Animal)TrueLooks like C's not just dog,c
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.