本文共 974 字,大约阅读时间需要 3 分钟。
classmethod:类方法
staticmethod:静态方法在python中,静态方法和类方法都是可以通过类对象和类对象实例访问。但是区别是:
@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'>
class A(object):
@classmethod def cm(cls): print '类方法cm(cls)调用者:', cls.__name__ @staticmethod def sm(): print '静态方法sm()被调用'class B(A):
passA.cm()
B.cm()A.sm()
B.sm()输出:
类方法cm(cls)调用者: A
类方法cm(cls)调用者: B 静态方法sm()被调用 静态方法sm()被调用转载地址:http://apfub.baihongyu.com/