Static property @property
. 1 class Mom: 2 Gender = " Woman " . 3 DEF the __init__ (Self, name, weight): . 4 the self.name = name . 5 self.weight = weight . 6 @Property # static method 7 # to a method made . 8 DEF cook_dinner ( Self): . 9 return " % S today be, cake " % (the self.name) 10 . 11 M1 = Mom ( ' the XFD ' , 120 ) 12 is #print (m1.cook_dinner ()) # this execution on the error 13 Print (m1.cook_dinner) # seems to be the call data attributes, in fact, calls the method 14 # the @Property hidden function attributes the role of external display , it seems to be the same data attributes
Static method: I did not feel with eggs
. 1 class Mom: 2 Gender = " Woman " . 3 DEF the __init__ (self, name, weight): . 4 the self.name = name . 5 self.weight = weight . 6 @staticmethod # . 7 DEF CLEAN_UP (A, B): # is not to self binding and examples . 8 return " the XFD was wiping S%, S% " % (A, B) . 9 DEF clean_up1 (A, B): 10 return " the XFD was wiping S%, S% " % (A, B) . 11 # 12 is Print (Mom.clean_up ( ' Desk ' , " stool " )) # the XFD was wiping tables, stools 13 is M1 = Mom ( " XFD " , 110 ) 14 Print (m1.clean_up ( ' Desk ' , " stool " )) # instance can invoke 15 # @staticmethod class toolkit 16 # do not bind with the class, instance, and does not bind . 17 # Mom.clean_up1 (1,2) is this call 18 is # m1.clean_up1 (1,2 ) can not call this: Why? 19 #(Put myself (m1) pass in when you call, but we certainly do not define an error cough) 20 # added @staticmethod and without what difference does it? 21 static methods only nominally classified management, you can not use class variables and instance variables
Class Methods
Requirements: do not use examples of how to call the function attribute class
class Mom: Gender = " Woman " def __init__ (Self, name, weight): self.name = name self.weight = weight # @ # static methods Property # to a method made @classmethod # method dedicated to the use of class def cook_dinner (CLS): # remember this keyword CLS # when using classmethod, to be devoted to define a class for this method call return " % S today to be a cake " % (cls.gender) # DEF cook_dinner (Self): # return "% s today to be a cake"% (self.name) Print (Mom.gender) M1 = Mom ( " XFD " , " 110 " ) # Print (Mom.cook_dinner (M1)) # this call to see. Print (M1.cook_dinner ()) # If you use the class methods can be called only by way of example # how do class methods, that is, not by example, direct calls it # @classmethod placed in front of this class class methods can be directly call (cls)
Note: The class method: I remember only call the class attribute data, which do not cls init data attributes. That is an example of use!