Method is not overloaded (instance method say_; function ())
In other languages, multiple methods with duplicate names can be defined as long as the method signature is unique. Method signature includes three parts: method name, parameter quantity and parameter type.
In Python, the parameters of a method have no declared type (the type of the parameter is determined when calling), and the number of parameters can also be controlled by variable parameters. Therefore, there is no method overload in Python. Defining a method can have multiple calling methods, which is equivalent to overloading methods in other languages. If we define multiple methods with duplicate names in the class body, only the last method is valid. Suggestion: do not use the method of duplicate names! Methods are not overloaded in Python.
The method of this place is say_hi is a duplicate, and only the last one will be executed.
#Method is not overloaded class Person: def say_hi(self):#Example method print("hello") def say_hi(self,name):#Example method print("{0},hello".format(name)) p1=Person() #p1. say_ The hi () method is repeated and will not be executed p1.say_hi("gaoqi")
Dynamics of method
Python is a dynamic language. We can dynamically add new methods to classes or dynamically modify existing methods of classes.
#Dynamics of test methods class Person: def work(self): print("Hard worker") def play_game(s):##Redefine methods outside the class print("Game fans{0}".format(s)) def work2(s): print("study hard") Person.play=play_game; p=Person() p.work() p.play() Person.work=work2 p.work()
Result display:
Hard worker
Game fans < main Person object at 0x00000212395B9E08>
study hard
infer other things from one fact:
class Person: def work(self): print("Hard worker") def play_game(self,name):##Redefine methods outside the class print("Game fans{0}".format(name)) def work2(self): print("study hard") p=Person() p.work() Person.play_game=play_game; p.play_game("gaoqi") Person.work=work2 p.work()
Result display:
Hard worker
Game fan gaoqi
study hard
Private properties and private methods (implementation encapsulation)
Python has no strict access control restrictions on class members, which is different from other object-oriented languages. About private attributes and private methods, there are the following points: 1 Generally, we agree that attributes starting with two underscores are private. Others are public. 2. Private properties (Methods) can be accessed inside the class 3 Private properties (Methods) cannot be accessed directly outside the class 4 Private properties (Methods) can be accessed outside the class through "class name _ private property (method) name"
[note] methods are also attributes in essence! It's just that it can be executed through (). Therefore, the private attributes and public attributes mentioned here also explain the usage of private methods and public methods. The following tests also include examples of private and public methods.
#Test private properties class Employee: def __init__(self,name,age): self.name=name self.__age=age##Set permissions and private attributes for the name def __work(self): ##Private method print("123) e=Employee("gao",18) print(e.name) print(e.age)##Change this to access print (e. _employee_age) e._Employee__work()
It can be accessed inside the class, which is equivalent to your own TV. You can control it with your own remote control.
@property decorator
@Property can change the calling mode of a method into "property call".
Can only be called and cannot be changed or reassigned
@property is mainly used to help us deal with the read and write operations of properties.
Inherited syntax format:
Class subclass (parent class 1. Parent class 2...) a subclass can inherit multiple parent classes.
If no parent class is specified, the default is object, which is the parent of all classes.
#Basic use of test inheritance class Person: #Define a class, which is also the parent class used later def __init__(self,name,age): #Instance properties self.name=name self.__age=age #Set age as a private property. Although subclasses inherit it, it cannot be called directly. def say_age(self): #Example method print("Age") class Student(Person): #Student is the subclass, Person is the parent, and the subclass inherits from the parent def __init__(self,name,age,score): #Instance properties Person.__init__(self,name,age)##Call the constructor of the parent class and pass in the subclass object. This step reflects the inheritance of the parent class self.score=score #Of course, name and age can also be written in this way, but in order to reflect the inheritance of the parent class, they are written in two ways. That is, the method in the previous line #Inheritance relationship, student -- > person -- > person -- > object (object is the parent class of all objects) print (Student.mro()) ##mro view the inheritance hierarchy of classes s=Student("gaoqi",18,60) s.say_age() print(s.name)#Pay attention to the use here Call function print(dir(s))#Properties of dir call object print(s._Person__age)
Inheritance and override of class members
1. Member inheritance: the subclass inherits all members of the parent class except the constructor. 2. Method Rewriting: subclasses can redefine the methods in the parent class, which will override the methods of the parent class, also known as "Rewriting"
#The difference between test inheritance and rewrite class Person: #Define a class, which is also the parent class used later def __init__(self,name,age): #Instance properties self.name=name self.__age=age def say_age(self): print("Age",self.__age) def say_introduce(self): print("My name is{0}".format(self.name)) class Student(Person): def __init__(self,name,age,score): Person.__init__(self,name,age) self.score=score def say_introduce(self): '''Override the method of the parent class''' print("my name is :{0}".format(self.name)) #As a result, this function will be called, overwriting the previous parent class s=Student("gh",18,60) s.say_age() s.say_introduce()
View the inheritance hierarchy of a class
Through class method mro() or class attribute__ mro__ You can output the inheritance hierarchy of this class.
object root class
Object class is the parent class of all classes, so all classes have the properties and methods of object class.
dir() view object properties
See all properties of the specified object.
print(dir(s2)
Rewrite__ str__ () method
Running result: the name is Gao Qi and the age is 18
multiple inheritance
Try to avoid using.
MRO()
Python supports multiple inheritance. If there are methods with the same name in the parent class, the interpreter will search in order "from left to right" when the child class does not specify the parent class name.
We can obtain the "class hierarchy" through the mro() method, and the method parsing order is also found according to the "class hierarchy".
super() gets the parent class definition
In the subclass, if we want to get the method of the parent class, we can do it through super(). super() represents the definition of the parent class, not the parent object.
Special method and operator overloading
Python operators are actually implemented by calling special methods of objects.
a=20 b=30 c=a+b d=a.__add__(b) print("c=",c) print("d=",d)
Operation result: c=50, d=50
Special properties
Python objects contain many double underlined start and end attributes. These are special attributes and have special usage.
Shallow and deep copies of objects
·Assignment of variables
Only two variables are formed, which actually point to the same object.
·Shallow copy
Python copies are generally shallow copies. When copying, the sub object content contained in the object is not copied. Therefore, the source object and the copy object will reference the same child object.
·Deep copy
Use the deepcopy function of the copy module to recursively copy the sub objects contained in the object. All child objects of the source object and the copy object are also different.
Design mode_ Factory mode implementation
Design pattern is the unique content of object-oriented language and a fixed practice when we face a certain kind of problems. There are many kinds of design patterns. The more popular ones are GOF (goupoof four) 23 kinds of design patterns. For beginners, we learn the two most commonly used modes: factory mode and singleton mode. The factory mode realizes the separation of creator and caller. Special factory classes are used to uniformly manage and control the selected implementation classes and creation objects.
Design mode_ Singleton mode implementation
The core function of singleton pattern is to ensure that there is only one instance of a class and provide a global access point to access the instance. Singleton mode only generates one instance object, which reduces the overhead of system resources. When the generation of an object requires more resources, such as reading configuration files and generating other dependent objects, a "singleton object" can be generated and then permanently stored in memory, which greatly reduces the overhead.