-
Java and C # only support object-oriented programming, and Python can support object-oriented or functional programming.
-
Object-oriented Two important concepts: 1. Class 2. Objects
-
Functional def sendemail (EMAIL,MSG): Functions
Print ("8888")
-
Object-oriented class Myclass:
def sendmail (self,email,msg): function definition in a class The first argument must be self
The print ("8888") function defines the called method in the class
5. Object-oriented first create the class and then create the object. A method in a class can only be called by an object of the class.
6. The essence of the first parameter self in an object-oriented method
Class SQLHelper:
def __init__ (self):
Pass
def query (Self,sql):
Print (Self.host)
Obj1 = SQLHelper ()///When a class name is appended with a parenthesis, the __ini__ () method in the class is automatically called
Obj1.host= "1.1"
Obj1.query (SQL)//obj1 calls the Query method in Sqlhepler, the first parameter in the Query method is the self representation obj1
Obj2 = SQLHelper ()
Obj2.host = "1.2"
Obj2.query (SQL)//obj2 calls the Query method in Sqlhepler, the first parameter in the Query method is the self representation obj2
The method called in the 7.python class does not have to be defined before the method is called (regardless of the order in which the method is defined, _init_ is performed first),
This is not the same as Python-functional programming (the called function must be defined after the called function, and the code executes sequentially from the top).
8. Functional application Scenarios--independent and no shared data between functions
Object-oriented scenario-there are a number of repeated calls between functions
Object-oriented encapsulation, in fact, is to use the construction method to encapsulate the content into the object, and then directly or self indirectly through the object
Get the encapsulated content
9. polymorphic
Parameters in a function in Python do not need to specify parameter types to receive arbitrary types of parameters, and automatically implement polymorphic
Other languages are inherited by the class, and the arguments in the function must be of the specified type or subtypes of the specified type to implement polymorphism
10.
This article from "Number One" blog, declined reprint!
Python Object-oriented programming