Knight Plan-python full stack 15 face object First Knowledge

Source: Internet
Author: User

Primary knowledge-oriented: process-oriented:

Advantage: Greatly reduce the complexity of the writing program, just follow the steps to be executed, stacking the code can be

Cons: A set of lines or processes is used to solve a problem, code reaching

Application scenario: Once you have completed a scene with little change. Examples of Linux kernels, git and apcaapache httpserserver, etc.

Object-oriented:

Advantages: To solve the extensibility of the program, the individual modification of an object, will be immediately reflected in the entire system, for example: The game of a character parameter characteristics and skill changes are easy

Disadvantage: Poor controllability (poor predictive results), unable to process-oriented programming pipeline can be very accurate prediction of the problem of the processing process and results.

Functions and object-oriented:

1. function encapsulates a function, while object-oriented encapsulation of multiple related functions

2. In the face of object comparison abstract, it is a kind of thought, need to stand in God's angle to understand

3. Program extensibility, objects are independent, coupling, diversity

Class: Refers to a class of objects with the same property or function (example human, reptile) object: is the concrete embodiment of the structure of the understanding: the face of the object: two parts
class A: #定义一个类     ' Alex ' # Static   properties, static variables, static fields.     def func1 (self):  #  function, dynamic property, method.         Pass
The two functions of the class: Property Reference and instantiation Property Reference:
classPerson:#Define a humanRole =' Person'  #People's role attributes are people    defWalk (self):#people can walk, that is, there is a way to walk        Print("Person is walking ...")Print(Person.role)#View the Role property of a personPrint(Person.walk)#referring to the way people walk, note that this is not the call
Class Name:

1.__dict__ Effect: View all properties and methods in a class and not be able to make any additional deletions

classPerson:animal='Advanced Animals'    defWork (self):Print('human beings all need work ... ')Print(person.__dict__)#properties in a classPrint(person.__dict__["Animal"])#through the __dict__ method of the individual properties and methods can be checked, but cannot be deleted or modified

2. View, (Add and remove) A class, some attributes with a universal point

Print (Person.animal)  # Check # print (person.language) # person.name = ' Alex '  # add #  print (person.name)#  person.animal = ' low animal ' # change #  del person.walk_way  # delete #  print (person.__dict__)

Note : You can perform dynamic properties (methods) in a class by person.__dict__["work"] (1), but this is not recommended

Instantiation: The class name is followed by parentheses, which triggers the operation of the __init__ (a special function) function, which can be used to customize its own characteristics for each instance

There are three stages inside the instantiation process:

1. Add a parenthesis to the class name ("Alex", "man"), it will open an object space in memory

2. Automatically executes the __init__ method in the class, and automatically passes the object space to the self parameter, and the other parameters ("Alex", "man") are manually passed in

3. Execute the __init__ method to encapsulate the appropriate properties for the object space

classPerson:#Define a humanRole =' Person'  #People's role attributes are people    def __init__(self,name,sex): Self.name= Name#each character has its own nickname;Self.sex = Sex#everyone has their own sex.            defWalk (self):#people can walk, that is, there is a way to walk        Print("Person is walking ...") Egg= Person ("Alex","Mans")#concrete instantiation, create a specific personPrint(Egg.name)#View the name of an objectPrint(Person.role)#View the Role property of a personPrint(Person.walk)#referring to the way people walk, note that this is not the call

Object:

Object Manipulation Object Space:

Object See all properties of object space with __dict__

An attribute of an object operand is modified with a universal point using Del to delete an attribute

Properties of object manipulation class space: can only be checked

Methods for manipulating the class space of objects (dynamic properties)

classPerson:animal='Advanced Animals'    def __init__(self,name,sex): Self.name=name Self.sex=SexdefWork (self):Print('human beings all need work ... ') obj= Person ("Alex","Mans")Print(obj.__dict__)#Check Object Space PropertiesObj.name ="Taibai"     #change of Object space propertyObj.hobby ="Play"    #increment of object space PropertiesdelObj.sex#Delete the sex attribute of the object spacePrint(Obj.animal)#Object Space View properties in class spaceObj.work ()#methods for manipulating class spaces with objects
class namespaces and Object namespaces

1. Why object space can invoke properties and methods in a class, only calls cannot be changed

  Object space first from its own space to find properties, object space without this property will be from the class object pointer from the class to find, if not found in the class, will be found from the parent class.

 An object can only view properties in a class and cannot be used to add or remove attributes from a class.

2. The class cannot invoke the properties of an object.

3. Objects and objects cannot be called from one another.

The same class of instantiated objects is not accessible to each other. But objects that are instantiated by different classes are likely to have mutual access.

4. Encapsulating attributes for objects: __init__ Anywhere

Knight Plan-python full stack 15 face object First Knowledge

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.