Analysis of Main object-oriented Python classes

Source: Internet
Author: User

In Python, a class defines an object. It contains information about the object action method, including its name, method, attribute, and event, the following article will further learn about the Python class, which greatly improves the variable data.

Objects can use common variables that belong to objects to store data. Variables belonging to an object or Python class are called fields. Objects can also use functions that belong to a class. Such functions are called class methods. Fields and methods can be collectively called class attributes. There are two types of domains-the method that belongs to each instance object) is called an instance variable; the method that belongs to the class itself is called a class variable.

There is only one special difference between class methods and common functions-they must have an additional first parameter name, but you do not assign a value to this parameter when calling a method, python provides this value. This special variable refers to the object itself, which is named self by convention.

The class and object methods can be understood as follows:

 
 
  1. Class Person:
  2. Population=0 
  3. Def _ init _ (self, name ):
  4. Self. name= Name
  5. Person. population + = 1
  6. Def _ del _ (self ):
  7. Person. population-=1 
  8. IfPerson. population= 0:
  9. Print ("I am the last one ")
  10. Else:
  11. Print ("There are still % d people left." % Person. population)
  12. Def sayHi (self ):
  13. Print ("hi my name is % s" % self. name)
  14. Def howMany (self ):
  15. IfPerson. population= 1:
  16. Print ("I am the only person here ")
  17. Else:
  18. Print ("we have % d persons here." % Person. population)
  19. S=Person("Jlsme ")
  20. S. sayHi ()
  21. S. howMany ()
  22.  
  23. K=Person("Kalam ")
  24. K. sayHi ()
  25. K. howMany ()
  26.  
  27. S. sayHi ()
  28. S. howMany ()
  29. Output:
  30. Hi my name is jlsme
  31. I am the only person here
  32. Hi my name is kalam
  33. We have 2 persons here.
  34. Hi my name is jlsme
  35. We have 2 persons here.

Population is a Python class variable. The name variable belongs to the object and uses self to assign values. Therefore, it is the variable of the object. We can see that the _ init _ method uses a name to initialize the Person instance. In this method, we increase population by 1 because we have added a person.

We can also find that the value of self. name is specified based on each object, which indicates the essence of self. name as the object variable. Remember, you can only use the self variable to refer to the variables and methods of the same object. This is called attribute reference.

  1. How to embed Python into C ++ applications?
  2. In-depth discussion of Ruby and Python syntax comparison
  3. Introduction to Python
  4. Python Learning Experience: version, IDE selection and coding Solutions
  5. Analysis of Python GIL and thread security

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.