What are the points for attention of python class methods
This article mainly explains "what are the points for attention of python methods". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "what are the points for attention of python methods?"
1. @ classmethod must be above the method.
2. The first cls must have, which refers to the class object itself.
3. Accessing instance properties and instance methods in class methods can lead to errors.
4. When a subclass inherits a parent method, cls is a subclass object, not a parent object.
Call class method format: "class name. class method name (parameter list)".
Values cannot be passed by cls in the parameter list.
Example
Class Person: # Class attribute school = "Sino-Canada Fenghua International School" tuition = 100000 count = 0 # instance attribute def _ _ init__ (self,name,age Gender): self.name = name self.age = age self.gender = gender Person.count = Person.count+1 @ classmethod def printSchool (cls): print (cls) print (cls.school) # instance method def get_score (self): print ("name: {0}" Age: {1}; gender: {2} ".format (self.name,self.age,self.gender) stu1 = Person (" sue ", 22," male ") stu1.get_score () Person.printSchool () so far, I believe you have a deeper understanding of" what are the points for attention of python methods ". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!