Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 对象不可调用_Python_Python 3.x_Typeerror_Callable - Fatal编程技术网

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