Short, object-oriented programming is to wrap a class class outside of a function program, and then point the class to an object
And the functions in class are called methods here.
As an example:
Use functional programming to write a script that sends a message in one click:
def e_mail (email,message): print (' mail sent ... ') return truee_mail (' [email protected] ', ' email content ')
Use object-oriented programming to write a script that sends a message in one click:
Class Foo: #e_mail称为方法 def e_mail (self,email, message): print (' message sent ... ') return true# Create an object; object name = Class name () obj = Foo () #通过对象去执行这个方法obj. E_Mail (' [email protected] ', ' email content ')
It is not difficult to find that object-oriented programming more than ordinary function programming a parameter self, What is this self?
Self is a parameter that Python automatically assigns to a value:
See Example:
Class SQL: #定义数据库的增删改查 def fetch (SELF,SQL): pass def create (self,sql): pass def remove (Self,nid ): Pass def modify (self,name): passobj =sql () obj.host= ' www.db1.com ' #定义数据库的host, The runtime then returns to the Selfobj.username= ' Python ' #定义数据库的用户名, which is then returned to the selfobj.password= ' 123 ' #定义数据库的密码, which is then returned to self ' When some functions have the same parameters, the parameter values can be assigned to the object one at a time using an object-oriented approach, and Python will return the parameters to the "#运行fetch方法 of self and point the arguments to SQL. Self inside has been returned by Python three parameters Obj.fetch (' select * from DB ')
Here is a basic understanding of the difference between functional programming and object-oriented programming:
When some functions have the same parameters, you can use object-oriented methods to assign the value of the parameter to the object at once, and Python will return the parameters to the self
Python programming: A comparison of functional programming and object-oriented programming