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

10 投票
4 回答
4641 浏览
提问于 2025-04-16 17:01

我可以定义一个Python的方法,让它同时既是静态方法又是实例方法吗?就像这样:

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

这样我就可以用这两种方式来调用它:

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

我的想法是能够运行两套逻辑。如果作为实例方法调用,它会使用实例变量来做一些事情。如果作为静态方法调用,它就不使用这些变量。

4 个回答

2

不行。如果你能这样做的话,self 在这个方法里面会是什么意思呢?

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
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)

撰写回答