Python中@classmethod 和@staticmethod用法和区别
@classsmethod
类装饰器:当用此装饰器定义方法时,将类而不是类的实例作为第一个参数,这意味着可以在此方法中直接使用类的属性,而不是特定的实例的属性,因此不必进行硬编码。
@staticmethod
静态装饰器:当用此装饰器定义方法时,不会传递类或实例作为它的参数,这意味着可以在类中放置一个函数。静态方法就是普通的函数,只是碰巧在类的定义体中,而不是在模块层定义。
在《流畅的Python》中,作者对这两个装饰器的评价:classmethod 装饰器非常有用,但是我从未见过不得不用 staticmethod 的情况。如果想定义不需要与类交互的函数,那么在模块中定义就好了。有时,函数虽然从不处理类,但是函数的功能与类紧密相关,因此想把它放在近处。即便如此,在同一模块中的类前面或后面定义函数也就行了。
class Date(object):
<span class="token keyword">def</span> <span class="token function">__init__</span><span
class="token punctuation">(</span>self<span class="token punctuation">,</span> day<span
class="token operator">=</span><span class="token number">0</span><span
class="token punctuation">,</span> month<span class="token operator">=</span><span
class="token number">0</span><span
class="token punctuation">,</span> year<span class="token operator">=</span><span
class="token number">0</span><span class="token punctuation">)</span><span
class="token punctuation">:</span>
self<span class="token punctuation">.</span>day <span class="token operator">=</span> day
self<span class="token punctuation">.</span>month <span class="token operator">=</span> month
self<span class="token punctuation">.</span>year <span class="token operator">=</span> year
@<span class="token builtin">classmethod</span>
<span class="token keyword">def</span> <span class="token function">from_string</span><span
class="token punctuation">(</span>cls<span class="token punctuation">,</span> date_as_string<span
class="token punctuation">)</span><span class="token punctuation">:</span>
day<span class="token punctuation">,</span> month<span class="token punctuation">,</span> year <span
class="token operator">=</span> <span class="token builtin">map</span><span
class="token punctuation">(</span><span class="token builtin">int</span><span
class="token punctuation">,</span> date_as_string<span
class="token punctuation">.</span>split<span class="token punctuation">(</span><span
class="token string">'-'</span><span class="token punctuation">)</span><span
class="token punctuation">)</span>
date1 <span class="token operator">=</span> cls<span class="token punctuation">(</span>day<span
class="token punctuation">,</span> month<span class="token punctuation">,</span> year<span
class="token punctuation">)</span>
<span class="token keyword">return</span> date1
@<span class="token builtin">staticmethod</span>
<span class="token keyword">def</span> <span class="token function">is_date_valid</span><span
class="token punctuation">(</span>date_as_string<span class="token punctuation">)</span><span
class="token punctuation">:</span>
day<span class="token punctuation">,</span> month<span class="token punctuation">,</span> year <span
class="token operator">=</span> <span class="token builtin">map</span><span
class="token punctuation">(</span><span class="token builtin">int</span><span
class="token punctuation">,</span> date_as_string<span
class="token punctuation">.</span>split<span class="token punctuation">(</span><span
class="token string">'-'</span><span class="token punctuation">)</span><span
class="token punctuation">)</span>
<span class="token keyword">return</span> day <span class="token operator"><=</span> <span
class="token number">31</span> <span class="token keyword">and</span> month <span
class="token operator"><=</span> <span class="token number">12</span> <span
class="token keyword">and</span> year <span class="token operator"><=</span> <span
class="token number">3999</span>
date2 = Date.from_string(‘11-09-2012’)
is_date = Date.is_date_valid(‘11-09-2012’)
使用静态方法写基类,注意静态方法使用了实例的硬编码。
class Date:
def __init__(self, month, day, year):
self.month = month
self.day = day
self.year = year
def display(self):
return “{0}-{1}-{2}”.format(self.month, self.day, self.year)
@staticmethod
def millenium(month, day):
return Date(month, day, 2000)
new_year = Date(1, 1, 2013) # Creates a new Date object
millenium_new_year = Date.millenium(1, 1) # also creates a Date object.
# Proof:
new_year.display() # “1-1-2013”
millenium_new_year.display() # “1-1-2000”
isinstance(new_year, Date) # True
isinstance(millenium_new_year, Date) # True
继承之后,millenium方法就用不了了。
class DateTime(Date):
def display(self):
return "{0}-{1}-{2} - 00:00:00PM".format(self.month, self.day, self.year)
datetime1 = DateTime(10, 10, 1990)
datetime2 = DateTime.millenium(10, 10)
isinstance(datetime1, DateTime) # True
isinstance(datetime2, DateTime) # False
datetime1.display() # returns “10-10-1990 - 00:00:00PM”
datetime2.display() # returns “10-10-2000” because it’s not a DateTime object but a Date object.
改成类方法,
@classmethod
def millenium(cls, month, day):
return cls(month, day, 2000)
就正确了
datetime1 = DateTime(10, 10, 1990)
datetime2 = DateTime.millenium(10, 10)
isinstance(datetime1, DateTime) # True
isinstance(datetime2, DateTime) # True
datetime1.display() # “10-10-1990 - 00:00:00PM”
datetime2.display() # “10-10-2000 - 00:00:00PM”
代码来自下面的链接,答的很赞:
https://stackoverflow.com/questions/12179271/meaning-of-classmethod-and-staticmethod-for-beginner/