【问题标题】:Static and instance methods in Python [duplicate]Python中的静态和实例方法[重复]
【发布时间】:2011-05-05 04:17:36
【问题描述】:

我可以将 Python 方法同时定义为静态和实例吗?比如:

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

这样我就可以同时调用它:

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

目的是能够运行两组逻辑。如果作为实例方法访问,它将使用实例变量并做一些事情。如果访问为静态方法,就不用了。

【问题讨论】:

标签: python static-methods descriptor instance-methods


【解决方案1】:
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)

【讨论】:

  • 你做到了。我不敢相信你做到了!
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    没有。如果可以的话,self 在方法内部是什么意思?

    【讨论】:

      【解决方案4】:

      如果您将self 参数删除到a(),您的代码将起作用。当您使用C().a(arg1) 调用它时,该实例将被忽略。

      但是您希望此方法既可用作静态方法,又可用作接收实例的方法。你不能同时拥有它。

      【讨论】:

      • 但是它会变成一个普通的静态方法。如果将其用作实例方法,我希望能够运行一组不同的逻辑并访问 self
      • 你最好在方法中传递你想要使用的变量。那么你不在乎它们是否是实例变量。
      猜你喜欢
      • 2018-03-07
      • 2011-10-25
      • 1970-01-01
      • 1970-01-01
      • 2015-01-09
      • 1970-01-01
      • 2012-11-12
      • 2012-08-13
      相关资源
      最近更新 更多