Definition of the Python3 class

Source: Internet
Author: User
Tags class definition

1. Process-oriented and object-oriented 1.1 process oriented

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

The advantage is that it greatly reduces the complexity of the writing process, and only needs to stack the code along the steps to be performed.

The disadvantage is: a set of pipeline or process is to solve a problem, code reaching.

1.2 Object-oriented

  The advantage is that 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.

Some nouns in object-oriented: Class, Object, instance, instantiation

Class: A category of things (people, dogs, tigers) with the same characteristics

Object/instance: A specific thing (next door Jen, Downstairs wong Choy)

Instantiation: Class-to-object procedure


2. Primary knowledge classes and objects

 Everything in Python is an object, and the nature of the type is the class.

 >>> dict #   Type Dict is class dict  <class  "  dict   " >> >> d=dict (Name= '  eva  " ) #   instantiate  >> > D.pop ( '  name   ") #   send a message to D, execute method of D pop   " eva   "  

In Python, a variable represents a feature, a function represents a skill, and thus a class of things with the same characteristics and skills is a ' class ', and the object is the specific one in this category.

2.1 Primary Knowledge class

(1) Define Class

#定义 the way of the function  def functionname (args):      ' function Document String '      
   #   How to define a class    class class Name: ' Document string ' class body   #   We create a class  class   Data:  pass   
# defining the properties of a function  class Person:   # define a human    'person'  #  Human role attributes are people (static properties under Class)    def Walk (self):  # People can walk, that is, there is a way to walk, also known as dynamic properties        Print("person iswalking ... ")

(2) attribute reference (class name. Properties)

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
 
classPerson:#Define a humanRole =' Person'  #People's role attributes are people    def __init__(self,name): Self.name= Name#each character has its own nickname;            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

Instantiation: The class name parentheses are instantiated, automatically triggering the run of the __INIT__ function, which can be used to customize each instance of its own characteristics

The process of instantiating is a class-to-object procedure

Originally we had only one person class, in this process, produced an egg object, has its own specific name, attack and health value.

Syntax: Object name = class Name (parameter)

Egg = person ('Egon')  # class name () is equal to executing person.__init__ ()#  An object is returned when the __init__ () is executed. This object is similar to a dictionary and has some properties and methods that belong to the person itself.  # You can secretly understand: Egg = {' name ': ' Egon ', ' Walk ': Walk}

(3) Viewing Properties & calling Methods

Print (egg.name)     # View Property Direct object name. Property name Print (Egg.walk ())   # call method, object name. Method Name ()

(4) About self

  Self: Automatically passes the object/instance itself to the first parameter of __init__ when instantiated, and you can give him an individual name, but the normal person will not do so.

(5) Class attribute supplement

A: Where do the properties of the class we define are stored? There are two ways to view Dir (class name): A Name list class name is detected.__dict__: A dictionary is found, key is the property name, value is the attribute two: Special Class attribute class name.__name__#the name of the class (string)The class name.__doc__#the document string for the classThe class name.__base__#the first parent class of a class (speaking of inheritance)The class name.__bases__#a tuple of all the parent classes of the class (speaking of inheritance)The class name.__dict__#Dictionary properties of the classThe class name.__module__#module where the class definition residesThe class name.__class__#class for instance (in modern class only)

(6) Summary

classclass Name:def __init__(self, parameter 1, parameter 2): Self. Property of Object 1=parameter 1 self. Properties of the Object 2=Parameter 2defMethod Name (self):Pass    defMethod Name 2 (self):PassObject Name= Class Name ($)#An object is an instance that represents a specific thing.                  #class name (): Class name + parenthesis is the instantiation of a class, equivalent to calling the __init__ method                  #Arguments in parentheses, parameters do not need to pass self, others correspond to parameter one by one in Init                  #The result returns an objectObject name. object's Properties 1#View the properties of an object, directly using the object name. Property nameThe name of the object. Method Name ()#call a method in the class, directly with the object name. Method Name ()

Definition of the Python3 class

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.