我试图在Python 3中继承torch.FloatTensor
并且无法调用超类__init__
。最小的例子:
import inspect
import torch
x = [[1, 2], [3, 4]]
print(torch.FloatTensor(x))
'''
1 2
3 4
[torch.FloatTensor of size 2x2]
'''
print(inspect.signature(torch.FloatTensor.__init__))
'''
(self, /, *args, **kwargs)
'''
class Image(torch.FloatTensor):
def __init__(self, arg):
print(inspect.signature(super().__init__))
super().__init__(arg)
Image(x)
'''
(*args, **kwargs)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-369-4b1aee6414f6> in <module>()
21 super().__init__(arg)
22
---> 23 Image(x)
<ipython-input-369-4b1aee6414f6> in __init__(self, arg)
19 def __init__(self, arg):
20 print(inspect.signature(super().__init__))
---> 21 super().__init__(arg)
22
23 Image(x)
TypeError: object.__init__() takes no parameters
'''
我对Python比较新,所以我可能会遗漏一些明显的东西。为什么在检查时显示object.__init__() takes no parameters
可能需要(*args, **kwargs)
?