Python中的静态方法和实例方法

10

我能否定义一个既是静态方法又是实例方法的Python方法?类似于:

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

这样我就可以同时使用两种方式调用它:

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

这个意图是为了能够运行两个逻辑。如果作为实例方法访问,它将使用实例变量并执行操作。如果作为静态方法访问,则不会使用实例变量。


这绝对是有趣的。我只是想知道是否有一种更少“hack”的方法来实现它。 - xster
4个回答

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)

你做到了。我简直不敢相信你做到了! - jathanism

4

formencode有一个classinstancemethod修饰符,可以实现您想要的功能。它要求该方法有两个参数(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

不行。如果你这样做,那么方法内的self将代表什么意思呢?


1

如果你从a()方法中删除self参数,你的代码将会正常工作。当你使用C().a(arg1)调用它时,实例会被忽略。

但是,你希望这个方法既可以作为静态方法,又可以作为接收实例的方法。你不能两者兼得。


但这样它就会变成一个普通的静态方法。我希望能够运行不同的逻辑并访问self,如果它被用作实例方法。 - xster
1
最好从方法内部传递要使用的变量。这样,您就不必关心它们是否为实例变量。 - jathanism

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接