Python full stack development "13th" Python Object-oriented

Source: Internet
Author: User
Tags class definition

first, the process : process-oriented program design is the core process (pipeline thinking), the process is to solve the problem, process-oriented design is like a well-designed pipeline, consider when to deal with what things.

Pros: Dramatically reduce the complexity of writing, just follow the steps, stack the code to

Disadvantage: A set of pipelines or processes is used to solve a problem, if the code changes will have to change

Ii. Object-oriented : the Thought of God

Advantages: It solves the extensibility of the program. A single modification of an object is immediately reflected in the entire system, such as the character of a character parameter in the game and the ability to modify it easily.

Cons: Poor controllability, inability to process-oriented programming pipelining can accurately predict the problem of the processing process and results, the object-oriented program once started by the interaction between the object to solve the problem, even God can not predict the end result. So we often see a game of people changes in a certain parameter is likely to lead to the ability of the bully to appear, a knife to kill 3 people, the game is out of balance.

class : A kind of thing with the same characteristics (man, dog, Tiger)

Object/instance: a specific thing

Instantiation: Class-to-object procedure (instance = class name (parameter 1, parameter 2))

Iv. Initial classes and objects

Classes: Class Person: #class class Name: Notice that the class name is followed by no parentheses

Class Body

In Python, a class of things that have the same characteristics and skills are ' classes ', using variables to represent features, to represent skills with functions,

Objects are specific to this kind of thing

Two functions of a class: Property Reference and instantiation

#属性引用class Person:    role = ' Chinese '  #类属性----static property    def walk (self):  #这里的函数叫做方法-----Dynamic Property        #注意: Self must write Print        (' person is walking ... ') print (person.role) #查看类属性print (person.walk) #查看类方法, call the method name with the class name and print out the memory address
#实例化class Person:    role = ' Chinese '    def __init__ (self,name,sex):        self.name=name        self.sex=sex    def Walk (self):        print (' walking ') hy = person (' Haiyan ', +) #实例化: The class name (parameter 1, parameter 2)  class name () is equal to the execution of the person.__init__ () print (hy.name)  #直接查看属性   object name. Property name Print (hy) #返回的是一个对象hy. Walk ()  #调用方法  , Object name. Method ()

Two functions of objects: viewing properties and calling methods (as described in the instantiation above)

V. About SELF

Self: The object/instance itself is automatically passed to the first parameter of __init__ when instantiated, and you can give it an individual name, but normal people don't do it because you don't know anyone else.

NOTE: def __init__ (self): This sentence can be written or not written, as long as there are parameters to enter the time must be written

def method Name (self): the self here must be written

VI. Special class Attributes

Print (Person.__name__,type (person.__name__)) #查看类的名字 (where the class name is a string type) print (person.__dict__) #查出的是一个字典, key is the property name, Value is the property values print (person.__doc__) # is to display the comments out of print (dir (person)) #查看类的方法, displayed as a list of print (person.__module__) # Module print (person.__class__) print (Isinstance (Hy,person)) where the class definition resides #判断对象 is an instance of the class

  

Vii. related knowledge of objects

1. The object is an example of a class that actually exists, i.e. the instance

2. Object/instance has only one function: Property reference

Egg = person (' Egon ', 10,1000) print (egg.name) print (egg.aggressivity) print (Egg.life_value) <br> of course, you can also refer to a method, Because the method is also a property.

  

Viii. Object-Oriented summary

Fixed format for definition and invocation

Class Name:    def __init__ (self, parameter 1, parameter 2): Self        . Property of the Object 1 = parameter 1 self        . property of Object 2 = parameter 2    def method name (self):p    Method Name 2 (self):p the name  of the #对象就是实例, which represents a specific thing                  #类名 (): Class name + parenthesis is the instantiation of a class, equivalent to invoking the __init__ method                  #括号里传参数, The parameter does not need to pass self, and the other is #结果返回一个对象对象名 corresponding to the formal parameter one by one in Init                  . The object's property 1   #查看对象的属性, directly with the object name. Method Name ()     #调用类中的方法, directly with Object name. Method Name () to

  

Ix. Interaction between objects

#人狗大战小程序class Person:    def __init__ (self,name,aggr,life_value):        self.name=name        self.aggr=aggr        Self.life_value=life_value    def attack (Self,dog): #人可以攻击狗        dog.life_value=dog.life_value-self.aggrclass Dog :    def __init__ (self,name,aggr,life_value):        self.name = name        Self.aggr = aggr        self.life_value = Life_ Value    def attack (Self,egg): #狗也可以攻击人        egg.life_value = Egg.life_value-self.aggregg = Person (' Egon ', 250,1000 Dog = Dog (' Lele ', 50,2000) print (' The dog did not attack before Egon's health: ', Dog.life_value)  #没攻击前egon的生命值egg. Attack (dog)   # Let Egon go to attack the dog Lele print (' The health of Egon after the dog attack: ', Dog.life_value)  #攻击后egon的生命值print (' Egon didn't attack the dog's health before: ', Egg.life_value)  #没攻击前egon的生命值dog. Attack (egg)   #让狗去攻击egonprint (' The Life of the dog after the Egon attack: ', Egg.life_value)  #攻击后egon的生命值

X. Class namespaces and objects, namespace of the instance

Creating a class creates a namespace for a class that stores all the names defined in the class, which become properties of the class

The class has two properties: static property and dynamic property

    • A static property is a variable that is defined directly in the class
    • A dynamic property is a method defined in a class

Python full stack development "13th" Python Object-oriented

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.