TypeError:__init __()缺少1所需的位置参数:' lists'
我创建了一个类,如下所示 -
class child:
def __init__(self,lists):
self.myList = lists
def find_mean(self):
mean=np.mean(self.myList)
return mean
当我创建一个像下面这样的对象时 -
obj=child()
它给出了错误 -
TypeError: __init__() missing 1 required positional argument: 'lists'
如果我创建如下所示的对象,那么它运行良好 -
obj=child([44,22,55)
或者如果我创建如下所示的类 -
class child:
def find_mean(self,myList):
mean=np.mean(myList)
return mean
然后我创建该对象像下面一样 -
obj=child()
那么它也可以很好地工作,但是我需要按照我在开始时解释的方式进行制作。你能帮我理解这个上下文吗?
I created a class, something like below -
class child:
def __init__(self,lists):
self.myList = lists
def find_mean(self):
mean=np.mean(self.myList)
return mean
and when I create an onject something like below -
obj=child()
it gives the error -
TypeError: __init__() missing 1 required positional argument: 'lists'
if I create object like below then it works well -
obj=child([44,22,55)
or If I create the class like below -
class child:
def find_mean(self,myList):
mean=np.mean(myList)
return mean
and then I create the object like below -
obj=child()
then also it works well, however I need to make it in the way I explained in the very begining. Can you please help me understand this context?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在第一个示例中,
__init__
方法需要两个参数:self
由 Python 自动填充。lists
是您必须提供的参数。它会尝试将此值分配给一个名为self.myList
的新变量,如果您不给它一个值,它将不知道应该使用什么值。在第二个示例中,您尚未编写
__init__
方法。这意味着 Python 创建了自己的默认 __init__ 函数,该函数不需要任何参数。但是,find_mean
方法现在要求您为其提供一个参数。当您说要按照一开始解释的方式创建它时,这实际上是不可能的:该类需要一个值,而您没有给它一个值。
因此,我很难说出你真正想做什么。但是,一种选择可能是您希望先创建该类,然后再向其中添加一个列表。在这种情况下,代码将如下所示:
此代码允许您先创建对象,然后再向其添加列表。如果您尝试调用
find_mean
而不给它一个列表,它只会返回nan
:In the first example, the
__init__
method expects two parameters:self
is automatically filled in by Python.lists
is a parameter which you must give it. It will try to assign this value to a new variable calledself.myList
, and it won't know what value it is supposed to use if you don't give it one.In the second example, you have not written an
__init__
method. This means that Python creates its own default__init__
function which will not require any parameters. However, thefind_mean
method now requires you to give it a parameter instead.When you say you want to create it in the way you explained at the beginning, this is actually impossible: the class requires a value, and you are not giving it one.
Therefore, it is hard for me to tell what you really want to do. However, one option might be that you want to create the class earlier, and then add a list to it later on. In this case, the code would look like this:
This code allows you to create the object earlier, and add a list to it later. If you try to call
find_mean
without giving it a list, it will simply returnnan
:您问题顶部的代码定义了一个名为
child
的类,该类具有一个属性lists
,该属性在中创建实例时分配>__init__
方法。这意味着您在创建child
实例时必须提供一个列表。如果您像第二个示例一样创建类,则不再显式指定
__init__
,因此,该对象没有必须在实例创建时分配的属性:两者都具有的唯一方法
myList
属性,并且不需要在创建时指定它,而是为其分配默认值:the code you have at the top of your question defines a class called
child
, which has one attribute,lists
, which is assigned at the time of instance creation in the__init__
method. This means that you must supply a list when creating an instance ofchild
.If you create the class like in your second example,
__init__
is no longer being explicitly specified, and as such, the object has no attributes that must be assigned at instance creation:The only way to both have the
myList
attribute, and not need to specify it at creation would be to assign a default value to it: