1.形式上的异同点:
在形式上,Python中:实例方法必须有self,类方法用@classmethod装饰必须有cls,静态方法用@staticmethod装饰不必加cls或self,如下代码所示:
1 class A(object): 2 def __init__(self, name): 3 self.name = name 4 5 def get_a_object(self): 6 return "get object method:{}".format(self.name) 7 8 @staticmethod 9 def get_b_static(): 10 return "get static method" 11 12 @classmethod 13 def get_c_class(cls): 14 return "get class method" 15 16 17 a = A(name="method") 18 print("{} by object method".format(a.get_a_object())) 19 try: 20 print(A.get_a_object()) 21 except Exception: 22 print("Class can not call the instance method directly by class method") 23 print("{} by class method".format(A.get_b_static())) 24 print("{} by object method".format(a.get_b_static())) 25 print("{} by class method".format(A.get_c_class())) 26 print("{} by object method".format(a.get_c_class()))
执行结果:
get object method:method by object method Class can not call the instance method directly by class method get static method by class method get static method by object method get class method by class method get class method by object method
从执行结果可以看出,实例方法必须实例化后调用不可用类点方法直接调用,静态方法和类方法既可以用实例点方法调用也可用类点方法直接调用
2.应用场景及本质上的异同点
对于应用场景上异同点主要是比较类方法和静态方法,先看一段代码:
1 class Date: 2 def __init__(self, year, month, day): 3 self.year = year 4 self.month = month 5 self.day = day 6 7 @staticmethod 8 def today_static(): 9 t = time.localtime() 10 return Date(t.tm_year, t.tm_mon, t.tm_mday) 11 12 @classmethod 13 def today(cls): 14 t = time.localtime() 15 return cls(t.tm_year, t.tm_mon, t.tm_mday) 16 17 18 class NewDate(Date): 19 pass 20 21 date = Date(2018, 12, 31) 22 new_data = NewDate(2018,12,31) 23 24 print("class method -------------") 25 print(date.today().__class__) 26 print(Date.today().__class__) 27 print(NewDate.today().__class__) 28 print(new_data.today().__class__) 29 print("\n") 30 print("static method ------------") 31 print(date.today_static().__class__) 32 print(Date.today_static().__class__) 33 print(NewDate.today_static().__class__) 34 print(new_data.today_static().__class__)
Date类有year、month、day三个属性,静态方法today_static,类方法today,NewDate类继承于Date,分别打印调用类方法和静态方法时属于哪个类
class method ------------- <class ‘__main__.Date‘> <class ‘__main__.Date‘> <class ‘__main__.NewDate‘> <class ‘__main__.NewDate‘> static method ------------ <class ‘__main__.Date‘> <class ‘__main__.Date‘> <class ‘__main__.Date‘> <class ‘__main__.Date‘>
通过结果可以看出:对于类方法today,调用Date.today()时cls=Date,调用NewDate.today()时,cls=NewDate,cls跟随调用类的变化而变化;对于静态方法today_static,指定了Date(t.tm_year, t.tm_mon, t.tm_mday),所以所属的类始终是Data,如果将Date改为:
class Date1: def __init__(self, year, month, day): self.year = year self.month = month self.day = day
此时对于@classmethod不会有任何变化,但是对于@staticmethod 如果today_static 返回值不是Date1(t.tm_year, t.tm_mon, t.tm_mday),则会报错
原文地址:https://www.cnblogs.com/FG123/p/9683510.html
时间: 2024-10-10 04:41:04