Static properties
@Property with behavior modification class into the class behavior attribute class, there are the role of the package
example.
# -*- coding: utf-8 -*- class Room: def __init__(self,name,owner,width,length,heigh): self.Name=name self.Owner=owner self.Width=width self.Length=length self.Heigh=heigh # Class into the behavior of the attribute class, an encapsulation effect @property def cal_area(self): return self.Length*self.Width = Room Room1 ( '102', 'Alex', 10,10,3)
# not called before adding @property # s=room1.cal_area() # print(s) room2=Room('103','alex',10,5,3) # s2=room2.cal_area() Print # (s2)
# after the call to add @property s1=room1.cal_area print(s1) s2=room2.cal_area print(s2)
Method (in the absence of the class method calls the class instantiation case) class
@classmethod
# -*- coding: utf-8 -*- class Room: tag=1 def __init__(self,name,owner,width,length,heigh): self.Name=name self.Owner=owner self.Width=width self.Length=length self.Heigh=heigh def cal_area(self): return self.Length*self.Width @classmethod def info(cls): print ( 'I am a class method') Room.info()
Static method
@staticmethod
Static method name only a home-based management, can not use the class variables and instance variables is based toolkit
# -*- coding: utf-8 -*- class Room: tag=1 def __init__(self,name,owner,width,length,heigh): self.Name=name self.Owner=owner self.Width=width self.Length=length self.Heigh=heigh def cal_area(self): return self.Length*self.Width @classmethod def info(cls): print ( 'I am a class method') @staticmethod def live(a,b): print ( '% s% s by households and'% (a, b)) # Class calls Room.live('alex','bob') Examples of call # room1=Room('102','alex',10,10,3) room1.live('alex','bob')
Summary: @property and only instance bindings (both static property can access an instance property, they can access the class attribute), @ classmethod only bind the class (class methods can only access the class attribute), @ staticmethod classes and instances can be (class class attributes neither method nor instance attributes)