TypeError:__init __()缺少1所需的位置参数:' lists'

发布于 2025-01-20 12:48:35 字数 719 浏览 4 评论 0原文

我创建了一个类,如下所示 -

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦初启 2025-01-27 12:48:35

在第一个示例中,__init__ 方法需要两个参数:

  • self 由 Python 自动填充。
  • lists 是您必须提供的参数。它会尝试将此值分配给一个名为 self.myList 的新变量,如果您不给它一个值,它将不知道应该使用什么值。

在第二个示例中,您尚未编写 __init__ 方法。这意味着 Python 创建了自己的默认 __init__ 函数,该函数不需要任何参数。但是,find_mean 方法现在要求您为其提供一个参数。

当您说要按照一开始解释的方式创建它时,这实际上是不可能的:该类需要一个值,而您没有给它一个值。

因此,我很难说出你真正想做什么。但是,一种选择可能是您希望先创建该类,然后再向其中添加一个列表。在这种情况下,代码将如下所示:

import numpy as np

class Child:
    def __init__(self, lists=None):
        self.myList = lists
        
    def find_mean(self):
        if self.myList is None:
            return np.nan
        mean = np.mean(self.myList)
        return mean

此代码允许您先创建对象,然后再向其添加列表。如果您尝试调用 find_mean 而不给它一个列表,它只会返回 nan

child = Child()
print(child.find_mean())  # Returns `nan`
child.myList = [1, 2, 3]
print(child.find_mean())  # Returns `2`

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 called self.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, the find_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:

import numpy as np

class Child:
    def __init__(self, lists=None):
        self.myList = lists
        
    def find_mean(self):
        if self.myList is None:
            return np.nan
        mean = np.mean(self.myList)
        return mean

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 return nan:

child = Child()
print(child.find_mean())  # Returns `nan`
child.myList = [1, 2, 3]
print(child.find_mean())  # Returns `2`
苏别ゝ 2025-01-27 12:48:35

您问题顶部的代码定义了一个名为 child 的类,该类具有一个属性 lists,该属性在 中创建实例时分配>__init__ 方法。这意味着您在创建 child 实例时必须提供一个列表。

class child:
    def __init__(self, lists):
        self.myList = lists
        
    def find_mean(self):
        mean=np.mean(self.myList)
        return mean

# works because a list is provided
obj = child([44,22,55])

# does not work because no list is given
obj = child()  # TypeError

如果您像第二个示例一样创建类,则不再显式指定 __init__ ,因此,该对象没有必须在实例创建时分配的属性:

class child:        
    def find_mean(self, myList):
        mean=np.mean(myList)
        return mean


# does not work because `child()` does not take any arguments
obj = child([44,22,55])  # TypeError

# works because no list is needed
obj = child()

两者都具有的唯一方法myList 属性,并且不需要在创建时指定它,而是为其分配默认值:

class child:        
    def find_mean(self,myList=None):
        mean=np.mean(myList)
        return mean


# now this will work
obj = child()

# as will this
obj = child([24, 35, 27])

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 of child.

class child:
    def __init__(self, lists):
        self.myList = lists
        
    def find_mean(self):
        mean=np.mean(self.myList)
        return mean

# works because a list is provided
obj = child([44,22,55])

# does not work because no list is given
obj = child()  # TypeError

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:

class child:        
    def find_mean(self, myList):
        mean=np.mean(myList)
        return mean


# does not work because `child()` does not take any arguments
obj = child([44,22,55])  # TypeError

# works because no list is needed
obj = child()

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:

class child:        
    def find_mean(self,myList=None):
        mean=np.mean(myList)
        return mean


# now this will work
obj = child()

# as will this
obj = child([24, 35, 27])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文