6

我想编写一个包装类,它接受一个值并且除了添加一个“原因”属性外,它的行为就像它一样。我有这样的想法:

class ExplainedValue(object):
    def __init__(self, value, reason):
        self.value = value
        self.reason = reason

    def __getattribute__(self, name):
        print '__getattribute__ with %s called' % (name,)
        if name in ('__str__', '__repr__', 'reason', 'value'):
            return object.__getattribute__(self, name)
        value = object.__getattribute__(self, 'value')
        return object.__getattribute__(value, name)

    def __str__(self):
        return "ExplainedValue(%s, %s)" % (
            str(self.value),
            self.reason)
    __repr__ = __str__

但是,双下划线函数似乎没有被捕获__getattribute__,例如:

>>> numbers = ExplainedValue([1, 2, 3, 4], "it worked")
>>> numbers[0]

Traceback (most recent call last):
  File "<pyshell#118>", line 1, in <module>
    numbers[0]
TypeError: 'ExplainedValue' object does not support indexing
>>> list(numbers)
__getattribute__ with __class__ called

Traceback (most recent call last):
  File "<pyshell#119>", line 1, in <module>
    list(numbers)
TypeError: 'ExplainedValue' object is not iterable

我认为以上两个最终应该这样做:

>>> numbers.value[0]
__getattribute__ with value called
1

>>> list(numbers.value)
__getattribute__ with value called
[1, 2, 3, 4]

为什么这没有发生?我怎样才能让它发生?(在实际代码中实际使用这可能是一个可怕的想法,但我现在对技术问题很好奇。)

4

2 回答 2

5

正如 millimoose 所说,一个隐式__foo__调用永远不会通过__getattribute__。您唯一能做的就是将适当的函数添加到您的包装类中。

class Wrapper(object):
    def __init__(self, wrapped):
        self.wrapped = wrapped

    for dunder in ('__add__', '__sub__', '__len__', ...):
        locals()[dunder] = lambda self, __f=dunder, *args, **kwargs: getattr(self.wrapped, __f)(*args, **kwargs)

obj = [1,2,3]
w = Wrapper(obj)
print len(w)

类主体是像任何其他块一样执行的代码(好吧,除了def);你可以把循环和任何你想要的东西放在里面。它们的神奇之处在于将整个本地范围传递到type()块的末尾以创建类。

locals()这也许是分配到甚至远程有用的唯一情况。

于 2013-01-24T23:37:36.703 回答
2

为了后代,这就是我想出的:

class BaseExplainedValue(object):
    def __init__(self, value, reason):
        self.value = value
        self.reason = reason

    def __getattribute__(self, name):
        if name in ('value', 'reason'):
            return object.__getattribute__(self, name)
        value = object.__getattribute__(self, 'value')
        return object.__getattribute__(value, name)

    def __str__(self):
        return "<'%s' explained by '%s'>" % (
            str(self.value),
            str(self.reason))
    def __unicode__(self):
        return u"<'%s' explained by '%s'>" % (
            unicode(self.value),
            unicode(self.reason))
    def __repr__(self):
        return "ExplainedValue(%s, %s)" % (
            repr(self.value),
            repr(self.reason))

force_special_methods = set(
    "__%s__" % name for name in (
        'lt le eq ne gt ge cmp rcmp nonzero call len getitem setitem delitem iter reversed contains getslice setslice delslice' + \
        'add sub mul floordiv mod divmod pow lshift rshift and xor or div truediv' + \
        'radd rsub rmul rdiv rtruediv rfloordiv rmod rdivmod rpow rlshift rrshift rand rxor ror' + \
        'iadd isub imul idiv itruediv ifloordiv imod ipow ilshift irshift iand ixor ior' + \
        'neg pos abs invert complex int long float oct hex index coerce' + \
        'enter exit').split(),
)

def make_special_method_wrapper(method_name):
    def wrapper(self, *args, **kwargs):
        return getattr(self, method_name)(*args, **kwargs)
    wrapper.__name__ = method_name
    return wrapper

def EXP(obj, reason="no reason provided"):
    if isinstance(obj, BaseExplainedValue):
        return obj

    class ThisExplainedValue(BaseExplainedValue):
        pass
    #special-case the 'special' (underscore) methods we want
    obj_class = obj.__class__
    for method_name in dir(obj_class):
        if not (method_name.startswith("__") and method_name.endswith("__")): continue
        method = getattr(obj_class, method_name)
        if method_name in force_special_methods:
            setattr(ThisExplainedValue, method_name, make_special_method_wrapper(method_name))

    ThisExplainedValue.__name__ = "%sExplainedValue" % (obj_class.__name__,)
    return ThisExplainedValue(obj, reason)

用法:

>>> success = EXP(True, "it went ok")
>>> if success:
        print 'we did it!'


we did it!
>>> success = EXP(False, "Server was on fire")
>>> if not success:
        print "We failed: %s" % (EXP(success).reason,)


We failed: Server was on fire

解释的值可以与它们包装的值互换使用:

>>> numbers = EXP([1, 2, 3, 4, 5], "method worked ok")
>>> numbers
ExplainedValue([1, 2, 3, 4, 5], 'method worked ok')
>>> numbers[3]
4
>>> del numbers[3]
>>> numbers
ExplainedValue([1, 2, 3, 5], 'method worked ok')

它甚至愚弄isinstance这里解释):

>>> isinstance(EXP(False), bool)
True
>>> isinstance(EXP([]), list)
True
于 2013-01-25T19:42:20.827 回答