Python programming: A comparison of functional programming and object-oriented programming

Source: Internet
Author: User

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

Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.