TypeError:__init __()缺少3个必需的位置参数?

时间:2020-02-15 19:15:22

标签: python

我最近启动了python并尝试编写简单的程序,但是此错误不断出现。我究竟做错了什么?

class Store:

 def __init__(self,name, id, price):
    self.name=name
    self.id=id
    self.price=price
    print("------------")

Store()
item1=Store(Chips, 10, 500)

输出

Traceback (most recent call last):
  File "C:/Users/aqsa/PycharmProjects/untitled5/list.py", line 8, in <module>
    Store()
TypeError: __init__() missing 3 required positional arguments: 'name', 'id', and 'price'

Process finished with exit code 1

谢谢。

1 个答案:

答案 0 :(得分:5)

您将Store类的构造函数定义为

 def __init__(self,name, id, price):
    self.name=name
    self.id=id
    self.price=price
    print("------------")

然后用Store()进行调用,它需要一个类似

的构造函数
 def __init__():
    self.name='default'
    self.id=0
    self.price=0
    print("------------")

摆脱Store()通话!

还,您是不是想将另一个构造函数称为Store('Chips', 10, 500)

给我们

class Store:

 def __init__(self,name, id, price):
    self.name=name
    self.id=id
    self.price=price
    print("------------")

item1=Store('Chips', 10, 500)