Object-oriented first knowledge of Python Foundation

Source: Internet
Author: User
Tags class definition

An object-oriented primary knowledge

Python is either process-oriented or object-oriented.

Concepts and advantages and disadvantages:

Process-oriented program design is the core of the process, 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 advantages are: greatly reducing the complexity of the program

The disadvantage is: a set of pipeline or process is to solve a problem, the production line of soda can not produce a car, even if it can, also have to be big change, change a component, reaching.

Application scenario: Once you have completed a very rarely changed scenario, the notable examples are Linux kernel, git, and Apache HTTP server.

Object-oriented program design is the core of the object, to understand what the object, we must regard themselves as God, God in the eyes of the world is the object of the existence of all things, not exist can also be created. Object-oriented programming good for example, to design a journey to the east, the Tathagata to solve the problem is to pass the scriptures to the Eastern Earth Datang, the Tathagata thought to solve this problem requires four people: Tang's monk, Sha Monk, Pig Eight commandments, Monkey King, everyone has their own characteristics and skills (this is the concept of objects, Characteristics and skills corresponding to the object's data properties and method properties, but this is not fun, so the Tathagata arranged a group of ghosts and goblins, in order to prevent the four of them in the road of learning to be killed, and arranged a group of immortal escort, these are objects. Then the beginning of the lessons, master and disciple four people with ghosts and goblins to interact until the final canon. The Tathagata does not control the four people follow what process to take.

Object-Oriented Programming

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.

Application scenario: frequently changing requirements of the software, the general needs of the changes are concentrated in the user layer, Internet applications, enterprise internal software, games, etc. are the object-oriented programming of the good place

Object-oriented programming is not all. For a software quality, object-oriented programming is just to solve extensibility.

Class II and objects

Everything in Python is an object, and Python3 unifies the concept of class and type, and the type is class, so whether you believe it or not, you've been using classes for a long time.

1>>> Dict#type dict is Class Dict2<class 'Dict'>3>>> D=dict (name='Luchuan')#instantiation of4>>> D.pop ('name')#send a message to D, execute method of D pop5 'Luchuan'6>>>

Based on object-oriented design a game: League of Legends, each player to choose a hero, each hero has its own characteristics and skills, characteristics that are data attributes, skill is the method of attributes, characteristics and skills of the combination of an object .

Extracting similar parts from a set of objects is a class , which is also a combination of features and skills, characterized by data and data shared by all objects, skill is a function attribute and is a function attribute shared by all objects.

Note: The real life, first has the original character, only then has the class.

In Python programming, there are classes first, and then objects such as Dict.

  The class is defined first, and the concrete object is produced by the class. The classes are extracted with common features and skill components. What equals what, this is characteristic. Skill is function.

Related knowledge of class Iii.

3.1 Primary Knowledge class

Declaring a function in Python is similar to declaring a class

declaring functions

1 def functionname (args): 2      ' function Document String ' 3       

declaring classes

 1   ""   2  class class name:  3  4   5   " 6   7  #   We create a class  8  class   Data:  9  pass  
Major premise:1. Only in Python2 want $ The new class and the classic class, the unification in Python3 is the new class 2. The biggest difference between the new class and the classic class declaration is that all modern classes must inherit at least one parent class 3. All classes do not explicitly declare the parent class, there is a default inheritance object parent class (speaking of inheritance will be said, first remember) in the python2 of the distinction between the Classic class:  class    name:Pass Classic class: class Class name (parent Class):     Pass in Python3, the two definitions are all of the new class and the new-style class

At the beginning of this section, it is concluded that a class is a combination of data and functions, which are called properties of a class

class Garen:        # defines the class of heroes Galen, different players can use it to instantiate their own heroes;    camp='demacia'  # All Players ' Heroes (Galen's) faction are demacia;    def Attack (self,enemy):   # common attack skill, enemy is the enemy;        # attack the enemy and lose the enemy's health according to their attack. 

The 3.2.1 class has two functions: attribute reference and instantiation

Property reference (class name. Properties)

# reference the Data property of a class, which is shared with all objects/instances ' Demacia ' # a function property that references a class that also shares <function garen.attack at 0x101356510>>>> garen.name='garen  '# Add Property del# Delete attribute

3.2.2 Instantiation (__init__ and self)

The class name parentheses are instantiated, which automatically triggers the operation of the __INIT__ function, which can be used to customize the characteristics of each instance.

classGaren:#define the class of the hero Galen, different players can use it to instantiate their own heroes;camp='Demacia'  #All Players ' Heroes (Galen's) faction are demacia;    def __init__(self,nickname,aggressivity=58,life_value=455):#Hero's initial attack ...;Self.nickname=nickname#The name of his own Galen;Self.aggressivity=aggressivity#heroes have their own attack;Self.life_value=life_value#heroes have their own health values;    defAttack (Self,enemy):#common attack skill, enemy is the enemy;Enemy.life_value-=self.aggressivity#attack the enemy and lose the enemy's health according to their attack. 

Instantiation: Class name + parentheses

>>> G1=garen (' Bush lun '# is in execution garen.__init__ (G1, ' Bush Lun ') and then executes __ init__ code inside the g1.nickname= ' Bush lun ' etc

The function of self is to automatically pass the object/instance itself to the first parameter of the __init__ when instantiated, and it can be any name, but it is not understood to write others blindly.

This automatic transfer mechanism is also reflected in the G1.attack call, followed by the introduction.

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)supplement to class attributes
supplement to class attributes

3.3 Object/instance has only one function: Property reference

Object-oriented first knowledge of Python Foundation

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.