TypeError: object.__new__() takes exactly one argument (the type to instantiate)

我的错误是:

Traceback (most recent call last):
  File "D:\anaconda3\envs\pytorch\lib\code.py", line 90, in runcode
    exec(code, self.locals)
  File "", line 25, in
  File "D:\anaconda3\envs\pytorch\lib\typing.py", line 872, in __new__
    obj = super().__new__(cls, *args, **kwds)
TypeError: object.__new__() takes exactly one argument (the type to instantiate)

看了几篇文章后发现问题所在,原来是底层文件的写法还是python2,但现在大家都用的是python

3了,python2中定义类的时候__new__()函数是需要返回值的,但是python3不需要了。

我的出错的地方在:

def __new__(cls, *args, **kwds):
    if cls in (Generic, Protocol):
        raise TypeError(f"Type {cls.__name__} cannot be instantiated; "
                        "it can be used only as a base class")
    if super().__new__ is object.__new__ and cls.__init__ is not object.__init__:
        obj = super().__new__(cls)
    else:
        obj = super().__new__(cls, *args, **kwds)#这里出现了返回值
    return objk

把后面的两个返回值删掉就行

你可能感兴趣的:(pytorch,人工智能,python)