Python中的静态和实例方法

时间:2011-05-05 04:17:36

标签: python static-methods descriptor instance-methods

我可以同时将Python方法定义为静态和实例吗?类似的东西:

class C(object):
    @staticmethod
    def a(self, arg1):
        if self:
            blah
        blah

所以我可以用它们来调用它:

C.a(arg1)
C().a(arg1)

目的是能够运行两组逻辑。如果作为实例方法访问,它将使用实例变量并执行操作。如果作为静态方法访问,它将不会。

4 个答案:

答案 0 :(得分:17)

import functools

class static_or_instance(object):
  def __init__(self, func):
    self.func = func

  def __get__(self, instance, owner):
    return functools.partial(self.func, instance)

class C(object):
  @static_or_instance
  def a(self, arg):
    if self is None:
      print "called without self:", arg
    else:
      print "called with self:", arg

C.a(42)
C().a(3)

答案 1 :(得分:4)

formencode有一个classinstancemethod装饰器,可以满足您的需求。它要求方法有2个参数(selfcls,其中一个可以根据调用上下文传递None

取自formencode/declarative.py

class classinstancemethod(object):
    """
    Acts like a class method when called from a class, like an
    instance method when called by an instance.  The method should
    take two arguments, 'self' and 'cls'; one of these will be None
    depending on how the method was called.
    """

    def __init__(self, func):
        self.func = func

    def __get__(self, obj, type=None):
        return _methodwrapper(self.func, obj=obj, type=type)

class _methodwrapper(object):

    def __init__(self, func, obj, type):
        self.func = func
        self.obj = obj
        self.type = type

    def __call__(self, *args, **kw):
        assert not kw.has_key('self') and not kw.has_key('cls'), (
            "You cannot use 'self' or 'cls' arguments to a "
            "classinstancemethod")
        return self.func(*((self.obj, self.type) + args), **kw)

    def __repr__(self):
        if self.obj is None:
            return ('<bound class method %s.%s>'
                    % (self.type.__name__, self.func.func_name))
        else:
            return ('<bound method %s.%s of %r>'
                    % (self.type.__name__, self.func.func_name, self.obj))

示例用法

class A(object):
    data = 5

    @classinstancemethod
    def print_(self=None, cls=None):
        ctx = self or cls
        print ctx.data


>>> A.print_()
5
>>> a = A()
>>> a.data = 4
>>> a.print_()
4

答案 2 :(得分:2)

没有。如果你能做到,self在方法中意味着什么?

答案 3 :(得分:1)

如果您将self参数移至a(),您的代码就会生效。当您使用C().a(arg1)调用它时,实例将被忽略。

但是您希望此方法既可以作为静态方法,也可以作为接收实例的方法。你无法双管齐下。