标签:style blog color sp div log ad ef bs
Static method静态方法属于类的方法,类的实例实际上不执行它
1 class Pizaa(object): 2 @staticmethod 3 def mix_ingredients(x,y): 4 return x+y 5 6 def cook(self): 7 return self.mix_ingredients(self.chess,self.vetetables) 8 9 10 >>> Pizaa.mix_ingredients is Pizaa().mix_ingredients 11 True 12 >>>
class method类的方法是绑定在类上的,不是实例。绑定到类的方法传入的第一个参数是这个类
1 class Pizaa(object): 2 radius = 42 3 4 @classmethod 5 def get_radius(cls): 6 return cls.radius 7 8 9 >>> Pizaa.get_radius 10 <bound method type.get_radius of <class ‘__main__.Pizaa‘>> 11 >>> Pizaa().get_radius 12 <bound method type.get_radius of <class ‘__main__.Pizaa‘>> 13 >>> Pizaa.get_radius is Pizaa.get_radius() 14 False 15 >>> Pizaa.get_radius is Pizaa().get_radius 16 False 17 \\>>> Pizaa.get_radius is Pizaa().get_radius 18 \\False 19 >>> Pizaa.get_radius() 20 42 21 >>>
标签:style blog color sp div log ad ef bs
原文地址:http://www.cnblogs.com/jypwn/p/4025316.html