Learning: Python learning path (6): Class

Source: Internet
Author: User
Learning: Python learning path (6): class and Object

In layman's terms, classes are definitions, and objects are entities.

Simply put, people are classes, and Gao Peng is an object.

Attribute

Attributes include instance attributes and class attributes.

Let's take a look at the previous Code:

  1. Class Fruit:
  2. Price = 0
  3.  
  4. Def _ init _ (self ):
  5. Self. color = 'red'
  6. Zone = "china"
  7.  
  8. If _ name __= = "_ main __":
  9. Print "Fruit price: % d" % Fruit. price
  10. Apple = Fruit ()
  11. Print "apple color: % s" % apple. color
  12. Print "apple price: % d" % apple. price
  13. Banane = Fruit ()
  14. Print "banane color: % s" % banane. color
  15. Print "banane price: % d" % banane. price
  16.  
  17. Fruit. color = "yellow"
  18. Fruit. price = 50
  19.  
  20. Print "apple color: % s" % apple. color
  21. Print "apple price: % d" % apple. price
  22. Print "banane color: % s" % banane. color
  23. Print "banane price: % d" % banane. price
  24. # Result
  25. Fruit price: 0
  26. Apple color: red
  27. Apple price: 0
  28. Banane color: red
  29. Banane price: 0
  30. Apple color: red
  31. Apple price: 50
  32. Banane color: red
  33. Banane price: 50

You can directly modify the class attributes of all classes through the class, but cannot modify the instance attributes.

Modify rows 17 and 18.

  1. Class Fruit:
  2. Price = 0
  3.  
  4. Def _ init _ (self ):
  5. Self. color = 'red'
  6. Zone = "china"
  7.  
  8. If _ name __= = "_ main __":
  9. Print "Fruit price: % d" % Fruit. price
  10. Apple = Fruit ()
  11. Print "apple color: % s" % apple. color
  12. Print "apple price: % d" % apple. price
  13. Banane = Fruit ()
  14. Print "banane color: % s" % banane. color
  15. Print "banane price: % d" % banane. price
  16.  
  17. Apple. color = "yellow"
  18. Apple. price = 50
  19.  
  20. Print "apple color: % s" % apple. color
  21. Print "apple price: % d" % apple. price
  22. Print "banane color: % s" % banane. color
  23. Print "banane price: % d" % banane. price
  24. # Result
  25. Fruit price: 0
  26. Apple color: red
  27. Apple price: 0
  28. Banane color: red
  29. Banane price: 0
  30. Apple color: yellow
  31. Apple price: 50
  32. Banane color: red
  33. Banane price: 0

Modifying instance variables only affects the instance variables and the class attributes of the instance.

Let's try again.

  1. Class Fruit:
  2. Price = 0
  3.  
  4. Def _ init _ (self ):
  5. Self. color = 'red'
  6. Zone = "china"
  7.  
  8. If _ name __= = "_ main __":
  9. Print "Fruit price: % d" % Fruit. price
  10. Apple = Fruit ()
  11. Print "apple price: % d" % apple. price
  12. Banane = Fruit ()
  13. Print "banane price: % d" % banane. price
  14.  
  15. Apple. price = 30
  16. Fruit. price = 50
  17.  
  18. Print "apple price: % d" % apple. price
  19. Print "banane price: % d" % banane. price
  20. # Result
  21. Fruit price: 0
  22. Apple price: 0
  23. Banane price: 0
  24. Apple price: 30
  25. Banane price: 50

If the class attribute of the instance is modified, it will be detached from the class attribute.

Class Method
  1. Class Fruit:
  2. Price = 0
  3.  
  4. Def _ init _ (self ):
  5. Self. color = 'red'
  6. Zone = "china"
  7. Def printColor (self ):
  8. Print "color:" + self. color
  9. Def printPrice (self ):
  10. Print "price: % d" % self. price
  11. @ Staticmethod
  12. Def printStatic ():
  13. Print "static"
  14.  
  15.  
  16.  
  17. If _ name __= = "_ main __":
  18. Fruit. printStatic ()
  19. Apple = Fruit ()
  20. Apple. printStatic ()
  21. Apple. printPrice ()
  22. Apple. printColor ()

The common method comes with the self parameter, which allows you to access instance attributes and class attributes through self. Static methods do not contain self parameters, nor can they access instance parameters and class parameters through self.

  1. Class Fruit:
  2. Price = 0
  3. @ Staticmethod
  4. Def printStatic ():
  5. Print Fruit. price
  6.  
  7. If _ name __= = "_ main __":
  8. Fruit. printStatic ()

It can only be accessed directly through a class.

Constructor and destructor
  1. Class Fruit:
  2. Count = 0
  3. Def _ init _ (self ):
  4. Print "I have been called"
  5.  
  6. Def _ del _ (self ):
  7. Print "I was unfortunately called"
  8.  
  9. If _ name __= = "_ main __":
  10. Apple = Fruit ()
  11. # Result
  12. I have been called
  13. Unfortunately, I was called.
  14.  
  15.  
  16. Class Fruit:
  17. Count = 0
  18. Def _ init _ (self ):
  19. Print "I have been called"
  20.  
  21. Def _ del _ (self ):
  22. Print "I was unfortunately called"
  23.  
  24. If _ name __= = "_ main __":
  25. Apple = Fruit ()
  26. Fruit. count
  27. # Result
  28. I have been called
  29. Unfortunately, I was called.

Class initialization and destruction are called.

The class is not automatically called when it is called.

Summary

This time I made some basic introductions to the class. Next time we will talk about inheritance and polymorphism.

 

 

 

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.