[英]__new__ = TypeError: object() takes no parameters
I wanted my class to be able to create objects by itself, and I saw this was possible with the ' new ', but apparently it's not working since I get this error : 我希望我的类能够自己创建对象,我看到这可能是' new ',但显然它不起作用,因为我收到此错误:
TypeError : object() takes no parameters TypeError : object()不带参数
So there is the class I'm working with, I do post all of it in case it may come from another part of the code. 所以有我正在使用的类,我会发布所有这些,以防它可能来自代码的另一部分。
class Planete:
def __new__(cls, rayon, periode, envergure,look):
print("test __new__ of the class {}".format(cls))
return object.__new__(cls, rayon, periode, envergure,look)
def __init__(self,rayon,periode,envergure,look):
self.rayon = rayon
self.periode = periode
#self.couleur = couleur
self.envergure = envergure
self.omega = (2*math.pi)/self.periode
self.i = 0
self.look = pygame.transform.rotozoom(look,0,self.envergure)
self.rect = pygame.Rect((0, 0), (0, 0))
#self.surf = pygame.Surface(self.rect.size)
def tourner(self) :
self.x = self.rayon*math.cos(self.omega*self.i)
self.x2 = int(self.x)+600
self.y = self.rayon*math.sin(self.omega*self.i)
self.y2 = int(self.y)+300
self.i = self.i + 1
self.rect = pygame.Rect((self.x2, self.y2), (50, 50))
def dessiner(self):
#pygame.draw.circle(gameDisplay,(self.couleur),((self.x2, self.y2)), self.envergure)
#gameDisplay.blit(self.surf,self)
gameDisplay.blit(self.look,(self.x2, self.y2))
The problem is exactly what the error message says: TypeError: object() takes no parameters
问题正是错误消息所说的:
TypeError: object() takes no parameters
In your __new__
method, you call object.__new__(cls, rayon, periode, envergure,look)
which is (with respect with the cls
parameter) what would be called for object(rayon, periode, envergure, look)
. 在你的
__new__
方法中,你调用object.__new__(cls, rayon, periode, envergure,look)
,这是(与cls
参数有关) object(rayon, periode, envergure, look)
。
The direct fix is to replace that line with : 直接修复是用以下内容替换该行:
return object.__new__(cls)
But anyway, as you have been said in comment, you have no real reason to use __new__
here: extract from the Python Language Reference: 但无论如何,正如你在评论中所说,你没有真正的理由在这里使用
__new__
:从Python语言参考中提取:
new () is intended mainly to allow subclasses of immutable types (like int, str, or tuple) to customize instance creation.
new ()主要用于允许不可变类型的子类(如int,str或tuple)自定义实例创建。
As your Planete
class is not an immutable type, and as you use __init__
for its customization, you should not use __new__
at all. 由于您的
Planete
类不是不可变类型,并且当您使用__init__
进行自定义时,您根本不应该使用__new__
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.