静态方法和成员方法分别在创建时分别被装入Staticmethod 类型和 Classmethod类型的对象中。静态方法的定义没有 self参数,且能够被类本身直接调用,类方法在定义时需要名为 cls的类似于self的参数,类成员方法可以直接用类的具体对象调用。但cls参数是自动被绑定到类的,请看下面例子:

__metaclass__ = type
class Myclass:
def smeth():
print "This is a static method"
smeth = staticmethod(smeth)
def cmeth(cls):
print "This is a class method of", cls
cmeth = classmethod(cmeth)

手动包装和替换方法的技术看起来有点单调。在Python2.4中,为这样的包装方法引入了一个叫做装饰器(decorators)的新语法(它能够对任何可调用的对象进行包装,即能够用于方法也能用于函数)。使用@操作符,在方法(或函数)的上方将装饰器列出,从而指定一个或者更多的装饰器(多个装饰器在应用时的顺序与指定顺序相反)

__metaclass__ = type
class Myclass:
@staticmethod
def smeth():
print "This is a static method"
@classmethod
def cmeth(cls):
print "This is a class method of", cls

定义了这些方法后,就可以像下面的例子那样使用(例子中没有实例化类):

>>>Myclass.smeth()
This is a static method
>>>Myclass.cmeth()
This is a class method of