10

作为一个学习练习,我正在尝试实现一个类,它将模拟 python 的complex内置行为,但具有不同的行为__str____repr__方法:我希望它们以格式打印......

(1.0,2.0)

...代替:

(1+2j)

我首先尝试简单地继承complex和重新定义__str__and __repr__,但这有一个问题,即当调用非覆盖方法时,complex会返回一个标准,并以标准格式打印:

>>> a = ComplexWrapper(1.0,1.0)
>>> a
(1.0,1.0)
>>> b = ComplexWrapper(2.0,3.0)
>>> b
(2.0,3.0)
>>> a + b
(3+4j)

当所需的输出是(3.0,4.0).

我正在阅读有关元类的信息,并认为它们可以解决我的问题。从Python Class Decorator中的答案开始,我目前的实现如下:

def complex_str(z):
    return '(' + str(z.real) + ',' + str(z.imag) + ')'
def complex_repr(z):
    return '(' + repr(z.real) + ',' + repr(z.imag) + ')'

class CmplxMeta(type):
    def __new__(cls, name, bases, attrs):
        attrs['__str__'] = complex_str
        attrs['__repr__'] = complex_repr
        return super(CmplxMeta, cls).__new__(cls, name, bases, attrs)

class ComplexWrapper(complex):
    __metaclass__ = CmplxMeta

不幸的是,这似乎与之前的解决方案具有相同的行为(例如,当两个ComplexWrapper实例相互添加时)。

我承认,我并不完全理解元类。也许我的问题可以用不同的方式解决?

当然,我可以手动重新定义相关的方法,例如__add____subtract__等。但这会非常重复,所以我更喜欢更优雅的解决方案。

任何帮助表示赞赏。


编辑:对 agf 的回答:

所以我对你的代码有很多不明白的地方:

  1. 元类的__new__方法ReturnTypeWrapper从哪里得到它的参数?如果它们是自动通过的,我希望在这种情况下name = "Complex", bases = (complex), dict = {}. 那是对的吗?这种自动传递类数据的方法是否特定于元类?

  2. 你为什么使用 cls = type.__new__(mcs, name, bases, dct)而不是 cls = type(mcs, name, bases, dct)?是否只是为了避免与 的“其他含义”混淆type()

  3. __str__我复制了你的代码,并__repr__在你的ComplexWrapper类中添加了我的特殊实现。但它不起作用;打印任何类型的对象Complex只会以标准 Python 格式打印。我不明白这一点,因为这两种方法应该在元类的 for 循环中被选中,但之后应该被我的定义覆盖。

我的代码的相关部分:

class Complex(complex):
    __metaclass__ = ReturnTypeWrapper
    wrapped_base = complex
    def __str__(self):
        return '(' + str(self.real) + ',' + str(self.imag) + ')'
    def __repr__(self):
        return '(' + repr(self.real) + ',' + repr(self.imag) + ')'

及其行为:

>>> type(a)
<class 'Cmplx2.Complex'>
>>> a.__str__
<bound method Complex.wrapper of (1+1j)>
>>> a.__str__()
'(1+1j)'
>>> 

再次感谢您的回答,如果您在回答中提到它们,请随时编辑/删除以上内容!

4

1 回答 1

9

你目前的方法行不通。你如何定义你的类不是问题——方法是在你调用它们时complex创建新实例complex,而不是使用type输入对象的。您将始终获取complex而不是的实例ComplexWrapper,因此不会调用您的自定义方法:

>>> type(ComplexWrapper(1.0,1.0) + ComplexWrapper(2.0,3.0))
<type 'complex'>

相反,您需要将complex的方法返回的新对象转换complex为派生类的返回对象。

这个元类包装了指定基类的所有方法,并将包装的方法附加到该类。包装器检查要返回的值是否是基类的实例(但不包括子类的实例),如果是,则将其转换为派生类的实例。

class ReturnTypeWrapper(type):
    def __new__(mcs, name, bases, dct):
        cls = type.__new__(mcs, name, bases, dct)
        for attr, obj in cls.wrapped_base.__dict__.items():
            # skip 'member descriptor's and overridden methods
            if type(obj) == type(complex.real) or attr in dct:
                continue
            if getattr(obj, '__objclass__', None) is cls.wrapped_base:
                setattr(cls, attr, cls.return_wrapper(obj))
        return cls

    def return_wrapper(cls, obj):
        def convert(value):
            return cls(value) if type(value) is cls.wrapped_base else value
        def wrapper(*args, **kwargs):
            return convert(obj(*args, **kwargs))
        wrapper.__name__ = obj.__name__
        return wrapper

class Complex(complex):
    __metaclass__ = ReturnTypeWrapper
    wrapped_base = complex
    def __str__(self):
        return '({0}, {1})'.format(self.real, self.imag)
    def __repr__(self):
        return '{0}({1!r}, {2!r})'.format(self.__class__.__name__, 
                                          self.real, self.imag)


a = Complex(1+1j)
b = Complex(2+2j)

print type(a + b)

请注意,这不会包装__coerce__特殊方法,因为它返回 a tupleof complexs; 如果需要,包装器可以很容易地转换为查看内部序列。

未绑定方法的__objclass__属性似乎没有记录,但它指向定义该方法的类,所以我用它来过滤掉定义在我们要转换的类以外的类上的方法。我在这里也使用它来过滤掉不是未绑定方法的属性。

于 2012-05-27T02:47:03.013 回答