【发布时间】:2012-05-15 23:56:56
【问题描述】:
我需要覆盖类中的插入符号行为,但我不确定哪个运算符重载适用于它。例如:
class A:
def __init__(self, f):
self.f = f
def __caret__(self, other):
return self.f^other.f
print A(1)^A(2)
此代码错误:
TypeError: unsupported operand type(s) for ^: 'instance' and 'instance'
如何构造类以便控制行为?
【问题讨论】:
-
我看到您使用的是 Python 2。您应该始终使用
class A(object):而不是class A:。永远不要使用旧式类。 -
这是为什么呢?不从对象继承有什么坏处?
标签: python operator-overloading caret