python--Create a more awesome object

Source: Internet
Author: User

What do you call a more awesome object? is actually a higher level object--managing objects

What is a Management object:
An object used to manage other objects that can tie everything together

The role of the management object:
Delegating work to other methods, simply delegating other methods to work in a well-defined order

All right! A bit of dizzy feeling! I thought of an example, such as a system patching.

Operating systems patching typically takes three steps:
1. Download Patches
2. Installation Patches
3. Restart the system

So, managing objects just make sure that these 3 steps happen sequentially!!
The following code is just a simulation of the event, see the following code:

class host:    def __init__ (SELF, HOSTNAME, IP):         self.hostname = hostname         self.ip = ip    def patch_repair (self):          #管理者方法 (patch fix Order)         self.download_patch ()   #下载         self.install_patch ()   #安装          self.reboot_system ()   #重启     def download_patch (self):          #补丁下载         print ("{}  Start downloading the patch ". Format (self.hostname))     def install_ Patch (self):         #补丁安装          print ("{} start&nbsP;install patch ". Format (Self.hostname))     def reboot_system (self):          #重启系统         print ("{} start  reboot system ". Format (self.hostname)) if __name__ ==  ' __main__ ':     h = host ("pc001",  "192.168.89.67")     h.patch_repair ()


There seems to be nothing wrong! So what are the benefits of doing this?
1, enhance readability, each step of the code is independent,
2, enhanced scalability, if the subclass just download the patch method, then you can directly inherit and overwrite it, rewrite it
3, enhance the isolation, an external class can create an instance of this class, you can directly use one of the methods, rather than Patch_repair

python--Create a more awesome object

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.