Python 对象不可调用
我正在尝试打印:Python 对象不可调用,python,python-3.x,typeerror,callable,Python,Python 3.x,Typeerror,Callable,我正在尝试打印: I am currently working on this code for my homework assignment involving Matrix Implementation: class MyMatrix(object): def __init__(self, n, m, t): self.n = n self.m = m self.t = t self.data = []
I am currently working on this code for my homework assignment involving Matrix Implementation:
class MyMatrix(object):
def __init__(self, n, m, t):
self.n = n
self.m = m
self.t = t
self.data = []
for i in range(0, self.n):
row = []
for i in range (0, self.m):
row.append(self.t())
self.data.append(row)
def set(self, i, j, v):
self.data[i][j] = v
def get(self, i, j):
self.data[i][j]
def __str__(self):
n = self.__class__.__name__ + "({})".format((self.n,self.m))
for i in range(0, self.n):
for j in range(0, self.m):
s += str(self.get(i,j)) + " "
s += "\n"
return s
class MySparseMatrix(MyMatrix):
def __init__(self, n, m, t):
self.n = n
self.m = m
self.t = t
self.data = {}
def set(self, i, j, v):
key = (i, j)
self.data[key] = v
def get(self, i,j):
key = (i, j)
return self.data.get(key, self.t())
但它给了我
tt = MySparseMatrix(int, 2, 2)
tt.set(0,0,11)
tt.set(0,1,5)
tt.set(1,0,2)
print(tt.get(0,1))
print("tt = ", tt)
关于如何修复此错误,有什么建议吗?我对Python非常陌生。您正在调用
self.t()
,它是一个整数tt=MySparseMatrix(int,2,2)
。将整数值2传递给实例变量t
。为变量找到一个有意义的名称可能有助于避免此类错误
例如:
TypeError: 'int' object is not callable
>a=1
>>>()
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
TypeError:“int”对象不可调用
最后一个返回行self.t()
出现问题,请删除Parantha。
t是一个整数,您正在尝试执行导致错误的方法调用 不要用废话填充问题,请包含完整的回溯,而不仅仅是错误消息。当问题编辑器提示您添加问题的更多细节时,请不要只发表胡言乱语。您的问题中缺少的一个关键细节是完整的回溯。将完整的回溯添加到您的问题中,逐字逐句。
>>> a = 1
>>> a()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not callable