程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python basics D8

編輯:Python

1 Method is not overloaded

Python in , The parameter of the method does not declare a type ( Determine the type of parameter when calling ), The number of parameters can also be determined by
Variable parameter control . therefore ,Python There is no method overload in . Defining a method can be called in many ways ,
It is equivalent to implementing the overloading of methods in other languages .

2. The dynamics of methods

Python It's dynamic language , We can dynamically add new methods to classes , Or dynamically modify the existing methods of the class .

3. Private properties and private methods ( Implement encapsulation )

Python There are no strict access control restrictions on class members , This is different from other object-oriented languages . About private ownership
Properties and private methods , There are the following points :
1. Usually we agree , Attributes starting with two underscores are private (private). Others are public (public).
2. Class can access private properties ( Method )
3. Private properties cannot be accessed directly outside the class ( Method )
4. Class can be passed “_ Class name __ Private property ( Method ) name ” Access private properties ( Method )
【 notes 】 Methods are essentially properties ! It's just that you can pass () Just execution

[email protected] Decorator

@property You can change the calling mode of a method into “ Calling a ”.

5. Attribute and method naming summary

· _xxx: Protection member , Out-of-service “from module import * ” Import , Only class objects and subclass objects can access
Ask these members .
· __xxx__: Special members of the system definition
· __xxx: Private members in class , Only class objects can access , Subclass objects are also inaccessible .( but , Out of class
The ministry can go through “ Object name . _ Class name __xxx” This special way to access .Python There are no strictly private members )
notes : Methods and properties follow the above rules .

6  Class coding style

1. Class names are capitalized , Use the hump principle between multiple words .
2. Instance name 、 The module name is in lowercase , Multiple words are separated by underscores .
3. Each class , Should follow “ docstring ”, Explain the function of this class .
4. You can organize code with blank lines , But don't abuse . In class , Use a blank line separation method ; Module , Use two
Empty lines separate multiple classes

7  Three characteristics of object orientation : Inherit 、 encapsulation ( hide )、 polymorphic

7.1  encapsulation ( hide )

Hide object properties and implementation details , Only provide necessary methods for external . Is equivalent to “ The details are encapsulated ”, only
External exposure “ Related call methods ”.
7.2 · Inherit
Inheritance can make subclasses have the characteristics of their parents , Improved code reuse .

Python Support for multiple inheritance , A subclass can inherit more than one parent class . The inherited syntax format is as follows :
class Subclass class name ( Parent class 1[, Parent class 2,...]):
The class body
If no parent class is specified in the class definition , The default parent class is object class .

When you define subclasses , The constructor of the parent class must be called in its constructor . The call format is as follows :
Parent class name .__init__(self, parameter list )

7.2.1  Inheritance and overriding of class members
1. Members inherit : The subclass inherits all members of the parent class except the constructor .
2. Method rewriting : Subclasses can redefine the methods in the parent class , This will override the methods of the parent class , Also known as “ rewrite

7.2.2  View the inheritance hierarchy of the class
Through the method of class mro() Or class properties __mro__ You can output the inheritance hierarchy of this class .

7.2.3 object The root class

object Class is the parent of all classes .

7.2.4 dir() View object properties

Built in functions dir(), It allows us to easily see all the properties of the specified object .

7.2.5  rewrite __str__() Method
object There is one __str__() Method , Used to return a for “ Description of the object ”, Corresponds to the built-in function str()
Often used for print() Method , Help us view the information of the object .__str__() Can be rewritten .

7.2.6  multiple inheritance
Python Support for multiple inheritance , A subclass can have multiple “ Immediate parents ”. such , You have “ Multiple fathers
class ” Characteristics . But because of , This will be “ The overall hierarchy of the class ” It's very complicated , Avoid using .

7.2.7 MRO()
Python Support for multiple inheritance , If there are methods with the same name in the parent class , When the subclass does not specify the parent class name , The interpreter will
“ From left to right ” Search in order .
MRO(Method Resolution Order): Method parsing order . We can go through mro() Methods to get
“ Class hierarchy ”, The method parsing order is also according to this “ Class hierarchy ” Looking for .

7.2.8 super() Get the parent class definition
In a subclass , If you want to get the method of the parent class , We can go through super() To do it .super() Represents the definition of the parent class , Not a parent object .

7.3  polymorphic
Polymorphism means that the same method call will produce different behavior due to different objects .

Note the following about polymorphism 2 spot :
1. Polymorphism is the polymorphism of method , Property is not polymorphic .
2. The existence of polymorphism has 2 Necessary conditions : Inherit 、 Method rewriting .

7.4  Special methods and operator overloads
Python The operator of is actually implemented by calling a special method of the object .

7.5  Shallow and deep copies of objects

· Assignment of variables
Just form two variables , It actually points to the same object .
· Shallow copy
Python Copies are generally shallow copies . When copying , The contents of sub objects contained in the object are not copied . therefore , Source object
And the copy object will reference the same child object .
· Deep copy
Use copy Modular deepcopy function , Recursively copies the child objects contained in the object . Source and copy objects
All child objects are also different .

7.6  Combine
“is-a” Relationship , We can use “ Inherit ”. So as to realize the methods and properties of the parent class owned by the child class .“is-a”
Relationship refers to a relationship like this : Dogs are animals ,dog is animal. Dogs should inherit animals .
“has-a” Relationship , We can use “ Combine ”, You can also implement that one class has methods and properties of another class .”
has-a” Relationship refers to such a relationship : Cell phones have CPU. MobilePhone has a CPU.

7.7  Design patterns _ Factory mode implementation

Design pattern is a special content of object-oriented language , It is our fixed practice when facing a certain kind of problem , Design
There are many patterns , What's more popular is :GOF(Goup Of Four)23 Design patterns .

The two most common patterns for beginners : Factory mode and singleton mode

7.8  Design patterns _ Singleton mode implementation

The singleton pattern (Singleton Pattern) The core role of is to ensure that a class has only one instance , And provide a
Global access points to access this instance .
Singleton mode generates only one instance object , Reduce the overhead of system resources . When an object is generated, it needs to compare
More resources , Such as reading configuration file 、 When other dependent objects are generated , Can produce a “ Singleton object ”, And then forever
Resident in memory , Thus greatly reducing the overhead .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved