The sixth day of the Python devops development

Source: Internet
Author: User

Python Object-oriented

Python has been an object-oriented language since its inception, and it is easy to create a class and object in Python.

Object-Oriented Introduction: Class, Class variables, object (base class), instance variables, constructors, encapsulation, inheritance, polymorphism, syntax attributes, destructors, private methods, private properties.

Programming Paradigm: Process-oriented, object-oriented, functional programming

Object-oriented programming (OOP programming): The use of "classes" and "objects" to create models to describe real-world things

First, the object-oriented introduction:

All things in the world can be classified, and all things in the world are objects.

As long as the object, it must belong to a category, as long as the object, there must be attributes.

Use object-oriented reason: it makes the maintenance and extension of the program easier, and improves the development efficiency.

Second, class (classes)

Definition: Abstraction of a class of objects with the same properties

class School (object):
n = 123 def __init__ (self,name,addr): =name =addr = [] = [] def enroll (self,stu_obj): print (" Register for learner%s "%Stu _obj.name) def Hire (self,straff_obj): print (" hire new Employee%s"% straff_ Obj.name)

1, class variables: The above n variable is a class variable, its value will be shared among all instances of this class, you can use in an inner class or an external class.

2. Constructor: Def __init__ (SELF,NAME,ADDR):

Role: Doing some initialization of classes at instantiation

3, instance variable: self.name = name, its scope is the instance itself,

4. Method function: Def enroll (self,stu_obj): #类的方法功能 (dynamic property)

5. Create an Instance object:

S1 = School ("Oldboy", "Shahe")

Third, class variables and instance variables

If the class variable is an instance variable with the same name, the instance variable is found when called.

The purpose of a class variable: an instance-sharing attribute that saves overhead.

classrole:n=123def __init__ (self,name): Self.name=Namer1= Role ("Jack") R2= Role ("Tom") Print ("r1:%s\nr2:%s"%(R1.N,R2.N)) print ("#". Center ( -,"#")) Print ("Change R1.N") R1.N="Change class variable"Print ("r1:%s\nr2:%s"%(R1.N,R2.N)) print ("#". Center ( -,"#")) Print ("Delete R1.N") del (R1.N) print ("r1:%s\nr2:%s"% (R1.N,R2.N))
View Code
r1:123R2:123################################################## Change R1.NR1: Change class variable R2: 123################################################## Delete r1.nr1:123R2: 123
Execution Results

Quad, destructor def __del__ (self):

performed automatically when an instance is released, destroyed, and usually used to do some finishing work, such as closing some database connections and closing open temporary files.

classrole:n=123#类变量, Def __init__ (Self,name,role,weapon,life_value= -, money=15000):        " "constructors, when instantiated, do some initialization work for classes" "Self.name =name #实例变量 (static property) Self.role=role Self.weapon=Weapon Self.__life_value= Life_value #私有属性, that is, life_value before adding"__"Self.money=Money def __del__ (self):" "Destructors , instance releases, automatic execution when destroyed, usually used to do some finishing work, such as closing some database connections and closing open temporary files:return:        " "Print"%s is completely dead."%self.name) def shot (self): print ('%s shot'%self.name) def got_shot (self): Self.__life_value-= -Print ('%s is got_shot'%self.name) def show_status (self): print ("name:%s,role:%s,life_value:%s"%(self.name,self.role,self.__life_value))
R1 = Role (' CJK ', ' Torrerist ', ' AK47 ')
r2 = Role (' xzmly ', ' police ', ' M43 ')
R1.got_shot ()
R1.show_status ()
del R1
R2.got_shot ()
Execution Result:
is Got_shotname:cjk,role:torrerist,life_value: - is got_shotxzmly completely dead.

V. Private methods, private properties

Private property: __private_attrs: Two underscores begin with, declaring that the property is a private property and cannot be used outside the class or accessed directly. Called in a method inside a class with Self.__private_attrs

Private method: __private_method: Two underscore begins, declares that the method is a private method and cannot be used outside the class or accessed directly. Called in a method inside a class with Self.__private_method

Vi. Succession

Inheritance can be fully understood as a type-to-subtype relationship between classes

Characteristics:

(1) In inheritance, the construction of a base class is not automatically called, it needs to be called specifically in the construction of its derived class.

(2) when calling a method of a base class, you need to prefix the class name of the base class with the Self argument variable, except that you do not need to take the self argument when calling a normal function in a class

(3) Python always looks for a method of the corresponding type first, and if it cannot find the corresponding method in the derived class, it begins to look up one by one in the base class. (Find the calling method in this class first, not found in the base class)

Inheritance notation:

people.__init__ (Self,name,age)

Equivalent to

This is recommended by super (Man,self) __init__ (Name,age), which makes it easier to modify a parent class when it changes.

Vii. Classical and new types

Classic class: Class people:

New class: Class people (object):

The difference is mainly embodied in multiple inheritance:

The classic class of Python2 is inherited by depth first, and the new class is inherited by the breadth-first class.

Python3 are all breadth-first

  

The sixth day of the Python devops development

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.