作为一个学习练习,我正在尝试实现一个类,它将模拟 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 的回答:
所以我对你的代码有很多不明白的地方:
元类的
__new__
方法ReturnTypeWrapper
从哪里得到它的参数?如果它们是自动通过的,我希望在这种情况下name = "Complex", bases = (complex), dict = {}
. 那是对的吗?这种自动传递类数据的方法是否特定于元类?你为什么使用
cls = type.__new__(mcs, name, bases, dct)
而不是cls = type(mcs, name, bases, dct)
?是否只是为了避免与 的“其他含义”混淆type()
?__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)'
>>>
再次感谢您的回答,如果您在回答中提到它们,请随时编辑/删除以上内容!