python-Object-Oriented Programming summary

Source: Internet
Author: User

What are the benefits of object-oriented programming?

1, the data and operations to change the function of data integration.

A: We used to operate the database in the following way:

1. Functions that define database operations.

2, each time the operation of the database to write the parameters and the contents of the operation.

#manipulating a database by invoking a functiondefExc1 (host,port,db,charset): Conn=Connect (host,port,db,charset) conn.execute (SQL)returnXXXdefExc2 (HOST,PORT,DB,CHARSET,PROC_NAME) Conn=Connect (host,port,db,charset) conn.call_proc (SQL)returnXXX#each call needs to pass through a bunch of arguments repeatedlyEXC1 ('127.0.0.1', 3306,'DB1','UTF8','select * from TB1;') Exc2 ('127.0.0.1', 3306,'DB1','UTF8','the name of the stored procedure')
manipulating databases through functions

  3, the above operation has a problem, that is, each time the operation is to pass a bunch of parameters.

even if it can be simplified, it can only be defined as a global variable, and the call is repeated to pass in a bunch of parameters.  
Host= ' 127.0.0.1' PORT=3306DB=' db1 ' CHARSET=' UTF8 'defExc1 (host,port,db,charset): Conn=Connect (host,port,db,charset) conn.execute (SQL)returnXXXdefExc2 (HOST,PORT,DB,CHARSET,PROC_NAME) Conn=Connect (host,port,db,charset) conn.call_proc (SQL)returnxxxexc1 (Host,port,db,charset,'select * from TB1;') Exc2 (Host,port,db,charset,'the name of the stored procedure')
by defining global variables

B, when we learn the class, we can do so.

#3, through the operation of the class, so that the data can be combined with the operation function. #3.1 Abstract The common attributes:     #host, port, DB#3.2 Abstract The common methods:     #Exc1, Exc2classMysqlhandler:def __init__(self,host,port,db,charset='UTF8'): Self.host=host Self.port=Port Self.db=DB Self.charset=CharSet Self.conn=Connect (self.host,self.port,self.db,self.charset)defExc1 (self,sql):returnself.conn.execute (SQL)defExc2 (self,sql):returnself.conn.call_proc (SQL)#4. Instantiate an object. Obj=mysqlhandler ('127.0.0.1', 3306,'DB1')#5. By manipulating this object, you can combine the data with the corresponding operating functionOBJ.EXC1 ('select * from TB1;') Obj.exc2 ('the name of the stored procedure')
manipulate by defining a class

2. High scalability

A. Extending data properties in a class

B. Extending the function properties in a class

classChinese:def __init__(self,name,age,sex): Self.name=name Self.age=Age Self.sex=SEXP1=chinese ('Egon', 18,'male') P2=chinese ('Alex', 38,'female') P3=chinese ('WPQ', 48,'female')
The original code of the class

classChinese:country=' China'    def __init__(self,name,age,sex): Self.name=name Self.age=Age Self.sex=SexdefTell_info (self): info=" "Nationality:%s Name:%s Age:%s Sex:%s" "%(Self.country,self.name,self.age,self.sex)Print(info) P1=chinese ('Egon', 18,'male') P2=chinese ('Alex', 38,'female') P3=chinese ('WPQ', 48,'female')Print(P1.country) p1.tell_info ()
After you add a method to a class

Note: The above operation we have added a method to the class, the original object does not have to make any changes, we can use the new class method. In this way, the scalability of the class is very high.     

python-Object-Oriented Programming summary

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.