Why Python object-oriented uses class

Source: Internet
Author: User

For non-programmer trained of the technical staff, understand a lot of development ideas in the field of things, in fact, is a very painful thing, until the touch of the class in Python, I understand what is object-oriented, more understanding or through the actual code practice can be slowly mastered.

Today, let's take a look at what is object-oriented and why classes are used in Python's programming world.

First, object-oriented

The process of stripping out the common features is object-oriented, which is my most intuitive understanding of object-oriented, such as the production of cars in the factory, each car has its own production platform, imagine such a scene, a car has 100 workers in the independent production, of which 20 people to produce a chassis, 20 people to develop the engine, 40 people to make the frame, the door, 20 people responsible for the final vehicle assembly, B car also has 100 people doing the same thing as a car, if the world is like this, Toyota, Volkswagen also has been closed, regardless of the model, although each car has its own independent production platform, But a lot of things must have the same properties, such as engine, steering wheel, saddle, tires, in small to nut and so on, we can put these common things completely independent of each model of the common attributes, then 100 workers may have 50 people are responsible for the public sector, saving labor costs, while It also saves on resource costs, such as the size of the seats that we need to adjust, so we just have to adjust the work of these 50 people, without having to operate each car independently, which is the benefit of the object-oriented.

Ii. object-oriented in Python-class

A simple example is the object-oriented production scenario, and we then compare the features in Python with functional programming and class programming.

For example, there is a simple need, is to develop a mail client program, can be customized through the program to send the content we want to send.

1. Function-Type programming

#  pseudo Code Def send_mail (to_user, title, content):     smtp_host =   "mail.test1.com"     smtp_username =  "Test_user1"      smtp_password =  "Test_password1"     smtp_obj = smtplib. SMTP (Smtp_host, smtp_username, smtp_password)     smtp_obj.sendmail (To_user,  title, content) Send_mail ("[email protected]",  "Test_mail",  "test_content") 

We use the above pseudo-code simple implementation of the mail delivery requirements, but if you want more people to use this feature, you will encounter some problems, such as the SMTP, the user password is not the same, we will change the code to look like the following.

#  pseudo Code Def send_mail (Smtp_host, smtp_username, smtp_password, to_user, title,  content):     smtp_obj = smtplib. SMTP (Smtp_host, smtp_username, smtp_password)     smtp_obj.sendmail (To_user,  title, content) Send_mail ("mail.test1.com",  "Test_user1",  "Test_password1",  "[email  protected] ", " Test_mail ", " Test_content "Send_mail (" mail.test2.com ", " Test_user2 ", " Test_password2 ", " [email protected] ", " Test_mail ", " Test_content ") Send_mail (" Mail.test3.com ", " Test_user3 ", " Test_password3 ", " [email protected] ", " Test_mail ",   "Test_content")

We also realize the function of multi-person send through the above way, the program writes here looks like there is no problem, but if our program needs to increase at this time, such as not just email, but also to receive, delete, and so on, our code becomes the following look.

#  pseudo Code Def send_mail (Smtp_host, smtp_username, smtp_password, to_user, title,  content):     smtp_obj = smtplib. SMTP (Smtp_host, smtp_username, smtp_password)     smtp_obj.sendmail (To_user,  title, content)     def recv_mail (smtp_host, smtp_username, smtp_ Password)     smtp_obj = smtplib. SMTP (Smtp_host, smtp_username, smtp_password)     # mail  receive code      ...    def delete_mail (smtp_host, smtp_username, smtp_ Password)     smtp_obj = smtplib. SMTP (smtp_host, smtp_username, smtp_password)     # mail  Delete features      ...    send_mail ("mail.test1.com",  "Test_user1",  "Test_" Password1 ", " [email protected] ", " Test_mail ", "Test_content") recv_mail ("mail.test1.com",  "Test_user1",  "Test_password1",  recv_argv,  ...) Delete_mail ("mail.test1.com",  "Test_user1",  "Test_password1",  delete_argv,  ...)

We see only one user want to achieve three functions, it is necessary to write so much code, this time the code is a bit cumbersome, and even more deadly if the address of the SMTP changed, I would like to change the parameters of each function function, which obviously increases the cost of work and the probability of errors, But we found that each feature has several common properties, that is, SMTP address, SMTP user, SMTP password, we can completely separate these three properties, through the encapsulation characteristics of the class implementation, that is, the following object-oriented programming.

2. Object-Oriented Programming

In Python, by creating classes, you can implement object-oriented programming ideas, as well as the above mail-sending program, which we use to implement the class.

#  pseudo Code class mailtools:    def __init__ (self, smtp_host, smtp_ Username, smtp_password):         self.smtp_host = smtp_ host        self.smtp_username = smtp_username         self.smtp_password = smtp_password         self.smtp_obj = smtplib. SMTP (Smtp_host, smtp_username, smtp_password)                  #  Send mail     def send_mail (self, to_ user, title, content):         self.smtp_obj.sendmail (To_user ,  title, content)             #  receive mail     def recv_mail (self, recv_argv,  ...)         # mail  Receive code     #  delete messages          def delete_mail (self, delete_argv,  ...)         # mail  Delete Code          test_mail = mailtools ("mail.test1.com",  "Test_user1",  "Test_password1") test_ Mail.send_mail (...) Test_mail.recv_mail (...) Test_mail.delete_mail (...)

We created the object Test_mail by the above-mentioned code, and then called the Send_mail, Recv_mail method, and so on, which makes our code logic clearer, if the SMTP parameter changes, We also only need to modify the parameters when the object is instantiated.

We use functional programming when it comes to simple functionality, because it is simple and fast enough, but as the program functions evolve and provide multiple complex logical operations, the function becomes less useful, so for different code scenarios, we need to use the programming of Python in a good way.

Knowledge points for classes in Python:

1. The "__init__" method is a construction method in Python that initializes our class, which is the common property we want to initialize, while the other functions in the class can be executed by the constructor.

2, Self is a formal parameter, for the instantiation of the class object, for example, a = My_class (), you can understand that a is the self parameter.

3. When creating an object, parentheses are required after the class, that is, the instantiation of the class is completed, and Python automatically finds the constructor method in that class.

4. If more than one function function is defined in a class, these functions can be called methods owned by the instantiated object of the class.

5. Three main features of object-oriented programming: encapsulation, inheritance, polymorphism


This article is from the "Record IT Operations" blog, be sure to keep this source http://abuve.blog.51cto.com/2237587/1795481

Why Python object-oriented uses class

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.