python将对象转换为int

Jet*_*tse 2 python integer casting object

我正在使用numpy模块来检索二维数组中最大值的位置.但是这个2d数组由MyObjects组成.现在我收到错误:

TypeError:unorderable类型:int()> MyObject()

我试图用这段代码覆盖int函数:

def int(self):
    return self.score
Run Code Online (Sandbox Code Playgroud)

但这并不能解决我的问题.我是否必须将我的2d MyObjects数组转换为2d整数数组,我是否必须扩展Integer对象(如果在python中可以这样做)或者我可以用另一种方式覆盖这个int()函数吗?

[编辑]

完整的对象:

class MyObject:
def __init__(self, x, y, score, direction, match):
    self.x = x
    self.y = y
    self.score = score
    self.direction = direction
    self.match = match

def __str__(self):
    return str(self.score)

def int(self):
    return self.score
Run Code Online (Sandbox Code Playgroud)

我称之为这个对象的方式:

 def traceBack(self):
    self.matrix = np.array(self.matrix)
    maxIndex = self.matrix.argmax()
    print(self.matrix.unravel_index(maxIndex))
Run Code Online (Sandbox Code Playgroud)

Kos*_*nos 10

尝试使用

...
def __int__(self):
    return self.score
...

test = MyObject(0, 0, 10, 0, 0)
print 10+int(test)

# Will output: 20
Run Code Online (Sandbox Code Playgroud)

在MyObject类定义中.