Aam*_*mir 4 python methods class
在Dive Into Python中,Mark Pilgrim说:
定义类方法时,必须将self显式列为每个方法的第一个参数
然后他在代码中给出了一些这样的例子:
def clear(self): self.data.clear()
def copy(self):
if self.__class__ is UserDict:
return UserDict(self.data)
import copy
return copy.copy(self)
Run Code Online (Sandbox Code Playgroud)
在网上浏览一些Python代码时,我遇到了@classmethod
装饰者.一个例子是:
class Logger:
@classmethod
def debug(msg):
print "DEBUG: " + msg
Run Code Online (Sandbox Code Playgroud)
(注意函数中没有self
参数debug
)
使用self
第一个参数和使用@classmethod
装饰器定义类方法有什么不同吗?如果没有,是一种定义更常用/优先的类方法的方法吗?
归档时间: |
|
查看次数: |
7060 次 |
最近记录: |