Python Object-oriented basics

Source: Internet
Author: User

Object-oriented Foundation 1. Briefly
    • Programming Method:

      • Process oriented: Execute from top to bottom according to the code in the stacking order of the script
      • Functional programming: Encapsulate code of the same functionality into functions, call directly, and reduce code repeatability
      • Object-oriented: classify and encapsulate functions, put similar functions into a class, make calls simpler
    • Why object-oriented?

      • Application needs to monitor the system's CPU, memory, hard disk, and so on, exceeding the threshold value of the alarm
      while True:    if cpu利用率 > 90%:        #发送邮件提醒        连接邮箱服务器        发送邮件        关闭连接    if 硬盘使用空间 > 90%:        #发送邮件提醒        连接邮箱服务器        发送邮件        关闭连接    if 内存占用 > 80%:        #发送邮件提醒        连接邮箱服务器        发送邮件        关闭连接

      As Python learns, it begins with functional programming

      def 发送邮件(内容)    #发送邮件提醒    连接邮箱服务器    发送邮件    关闭连接while True:if cpu利用率 > 90%:    发送邮件('CPU报警')if 硬盘使用空间 > 90%:    发送邮件('硬盘报警')if 内存占用 > 80%:    

      Functional programming increases the readability and reusability of the code, but this is only a single machine monitoring, and if I want to monitor more than one, it may need to be written like this:

def 发送邮件(内容)        #发送邮件提醒        连接邮箱服务器        发送邮件        关闭连接     while True:        for host in host-list:  #通过遍历host列表来进行监控            if cpu利用率 > 90%:                发送邮件('CPU报警')                     if 硬盘使用空间 > 90%:                发送邮件('硬盘报警')                     if 内存占用 > 80%:                发送邮件('内存报警')     

This seems to be achieved, but if it is 1000 machines, perhaps when the cycle to 999 units, the 100th has been a problem. Cause alarm delay. And what if you use object-oriented? will be a good solution to this problem

class host:    def 发送邮件(内容)        #发送邮件提醒        连接邮箱服务器        发送邮件        关闭连接     def judge(self):        while True:            if cpu利用率 > 90%:                发送邮件('CPU报警')                     if 硬盘使用空间 > 90%:                发送邮件('硬盘报警')                     if 内存占用 > 80%:                

I created each machine as an object with the method above, so that I could monitor multiple threads.

2. Object-oriented
  • The
  • Class and the object
    class are templates that can contain multiple methods (that is, functions) that implement functions in the method, objects are instances created from templates, and instances of objects can execute functions in classes

    • Create classes and objects
      # Create Class class+ class Foo: #class是关键字, representing Class, Foo is the name of the class Def F1 (self): # Method of Class 1 pas S def f2 (self): # Method of Class 2 pass# Create Object object = Class name () bar = foo () # Creates a bar object that has all the methods in the class, creates the object, and then parentheses the class name The #调用对象的方法 object. Method Name () Bar.f1 () bar.f2 ()  
    • Example:
      # Create class Classes Sql:def creates (self,sq L): Print (SQL) def modify (self, SQL): Print (SQL) def remove (Self,sql): Print (SQL) def fetch ( Self,sql): Print (SQL) # Create object obj1 = SQL () obj2 =sql () #调用对象里的方法 res1 = obj1.modify (' modify ') Res2 = Obj2.fetch (' view ') PRI NT (' obj1: ' res1) print (' Obj2: ' Res2) Run Result: obj1: Modify Obj2: View  
    • Scenario

      • Object-oriented: "Create object "" Through the object execution method, when some functions have the same parameters, you can use an object-oriented way, the parameter values are encapsulated into the object, the function is directly called to
      • function programming: "Execute function" functions are independent and no common data
  • What the hell is self in the class?
    Self is a formal parameter to the automatic value of Python, and the object that invokes the method automatically executes self, in a class where self is the object itself
    Also use the above example, if I need to execute the method, verify the user name, password pass, the validation passed before executing the method inside

class SQL:            def create(self,sql):            print(sql)            def modify(self, sql):            print(sql)            def remove(self,sql):            print(sql)            def fetch(self,sql):            print(sql)          #创建对象     obj1 = SQL()     obj1.user = 'user'     obj1.password = 'password'     res1 = obj1.modify('修改')          obj1 =SQL()     obj1.user = 'user'     obj1.password = 'password'          res2 = obj2.fetch('查看')

Each object invocation method is found to be assigned to both user and password. Very troublesome. This method is automatically executed when the object is created using the Init method of the class.

class SQL:         def __init__(self,user,password):              self.user = user              self.password = password        def create(self,sql):            print(sql)            def modify(self, sql):            print(sql)            def remove(self,sql):            print(sql)            def fetch(self,sql):            print(sql)obj = SQL('username','password')

Python Object-oriented basics

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.