http://www.cnblogs.com/chenzehe/archive/2010/09/01/1814639.html
classmethod:类方法
staticmethod:静态方法
在python中,静态方法和类方法都是可以通过类对象和类对象实例访问。但是区别是:
Example 1:
>>> class a():
@staticmethod
def staticm():
print 'static'
def normalm(self):
print 'nomarl',self
@classmethod
def classm(cls):
print 'class',cls
>>> a1=a()
>>> a1.normalm()
nomarl <__main__.a instance at 0x84dddec>
>>> a1.staticm()
static
>>> a1.classm()
class __main__.a
>>> type(a)
<type 'classobj'>
>>> type(a1)
<type 'instance'>
Example 2:
class A(object):
@classmethod
def cm(cls):
print '类方法cm(cls)调用者:', cls.__name__
@staticmethod
def sm():
print '静态方法sm()被调用'
class B(A):
pass
A.cm()
B.cm()
A.sm()
B.sm()
输出:
类方法cm(cls)调用者: A
类方法cm(cls)调用者: B
静态方法sm()被调用
静态方法sm()被调用