The first knowledge of Python classes and objects

Source: Internet
Author: User

Class

What is an object? Before we ask for the length of a string, solved by a for loop or while loop, as the course progresses, we can implement it by calling the function, with the specific code to implement the following:

 1  #   Use for loop  2  s1 =  '  cfhgjrfekdcmfvjrgte  "  3  Count = 0  4   For  i in   S1:  5  count + 16  print  (count) 
 1  #   Using function implementation:  2  def   Mylen (argv):  3  count = 0  4  for  i in   argv:  5  count + = 16  return   count  7  s1 =  " cfhgjrfekdcmfvjrgte    8  9   Print  (Mylen (S1)) 

Now let's look at an example :

1 class A: 2     ' Colin ' 3     def func1 (self): 4         Pass

This class, which contains class definitions, is called classes. Structurally, the above can be divided into two parts:

1 such as: name = ' Colin ' This part is called: static property, static variable, also can be called static method

2 such as: Def defines a function, which we call dynamic properties, which can also be called methods.

What is the difference between a function and an object-oriented one? Let me give you the following two examples:

Shopping cart system, in order to achieve registration, landing and shopping different functions, use the function:

def Register (argv):     Pass def Login (argv):     Pass def Shoppingcar (argv):     Pass

Use object-oriented to put individual function functions into a class:

class Shopping_car:     def __init__ (self):  #  Special Method        pass    def  Register (self):          Pass    def  login (self):        pass    def  Shoppingcar (self):        Pass

From the above comparison, we can see:

The 1 function encapsulates a function, while object-oriented encapsulation is a number of related functions

2 Object-oriented abstraction, which is an idea

3. Object-oriented programs are extensible, and objects are independent. It is coupled and differentiated

We define a class, how do we see all the properties and methods in the class?

1 Here is a way to provide __dict__

classPerson:animal='Advanced Animals'Walk_style='Upright Walking'language='language'    defEat (self):Print('It's time to eat, drink, and do not forget your heart')    defWork (self):Print('human beings all need work ... ')Print(person.__dict__)Print(person.__dict__['name'])#the error can be checked by means of the individual properties and methods of __dict__, but cannot be deleted or modified .Person.__dict__['Animal'] ='low-grade animals'   #error TypeError: ' Mappingproxy ' object does not support item assignmentPerson.__dict__['name'] ='Alex'   #error, cannot increase

Therefore, as can be seen from the above example, the general use of __dict__ to see all the properties and methods in the class, no other operation.

2 The second method of viewing, using '. ', the use of the point, in some cases can be implemented to achieve the deletion of the class.

 print  (person.animal) #   Check advanced animal  person.name = "  colin     " #   increase Colin  print   (person.name) Person.animal  =  "  low animal   '  

3 How to: generally do not operate through the class name, so you need to introduce an object.

Object

What is an object? First introduce the following example:

classPerson:animal='Advanced Animals'Walk_way='Upright Walking'language='language'    def __init__(Self,name,age,eye):#function: Encapsulates properties for an object. Self.name =name Self.age=Age Self.eye= EyedefEat (self):#print (self)        Print('It's time to eat, drink, and do not forget your heart')    defWork (self):Print('human beings all need work ... ') obj= Person ('Colin', 27,'Small Eyes')#This process is an instantiation process that instantiates an object (he instantiates an object space in memory). Print(obj)Print(Obj.name)
There are three stages within the instantiation process:
1, opens up 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 other parameters are passed in manually.
3, execute the __init__ method to encapsulate the appropriate properties for the object space.

Object manipulation refers to the operation of object space, the same as the class operation, by adding or deleting the operation.

Object View object space all properties using:__dict__
Print (obj.__dict__)
object to manipulate a property of the object space through the '. ' To realize the function of the increase and change check.
' male '  # Increase del obj.eye1  #  Delete ' big bit '  #  Change  Print(obj.name1)print(obj.__dict__)
the properties of the object manipulation class space can only be checked .
  print ' Low-level animal ' print (obj.animal) print (obj.  __dict__)print(person.  __dict__)
Methods for manipulating class spaces with objects
Obj.eat ()

The first knowledge of Python classes and objects

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.