Python 缺少 1 个必需的位置参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24896613/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 05:25:45  来源:igfitidea点击:

Missing 1 required positional argument

pythonclasstypeerror

提问by XallZall

let me begin with that I am as green as it gets when it comes to programming but have been making process. My mind however still needs to fully understand why and what is happening.

让我开始说,在编程方面我是绿色的,但一直在制作过程。然而,我的头脑仍然需要完全理解为什么以及发生了什么。

Anyways the issue at hand as the Title suggests, I will paste the source code which I will keep to a minimum.

无论如何,正如标题所暗示的那样,我将粘贴源代码,并将其保持在最低限度。

(I use Python Version 3.4.1)

(我使用 Python 版本 3.4.1)



class classname:
    def  createname(self, name):
        self.name = name;
    def displayname(self):
        return self.name;
    def saying(self):
        print("Hello %s" % self.name);

first = classname;
second = classname;

first.createname("Bobby");


I will paste here the error code:

我将在这里粘贴错误代码:



Traceback (most recent call last):
File "<pyshell#16>", line 1, in <module>
first.createname("Bobby")
TypeError: createname() missing 1 required positional argument: 'name'


I understand that I need to check the errors and read carefully what it says as well as do a search before a post, with that done and failing to solve the problem I come here to post it...

我知道我需要检查错误并仔细阅读它所说的内容以及在发布之前进行搜索,完成后未能解决问题我来这里发布...

From here on you can read if you are interested about what I think is going on in my mind:

从这里开始,如果您对我的想法感兴趣,可以阅读以下内容:

The error tells me that I need 1 more argument in the name, so I must be going wrong there, but I already tried something like this:

错误告诉我名称中还需要 1 个参数,所以我一定是哪里出错了,但我已经尝试过这样的事情:

first.createname("bobby", "timmy");

Having more arguments in name? If I understand correctly an argument is in this --> () <--

名义上有更多争论?如果我理解正确的话,这里有一个参数 --> () <--

I also rule out the fact that it would be the def createname(self, name), because self is or should be alone and not included? So I do not really understand what is going on.

我还排除了它是 def createname(self, name) 的事实,因为 self 是或应该是单独的而不包括在内?所以我真的不明白发生了什么。

Thank you in advance and sorry if this has been answered already, in which case I must have overlooked it.

预先感谢您,如果已经回答了这个问题,很抱歉,在这种情况下,我一定忽略了它。



SOLVED:

解决了:

 first = classname;
 second = classname;

Should be:

应该:

 first = classname();
 second = classname();

The parentheses where missing which of course means I just made first and second EQUAL to something else and not link it with the "actual cl

缺少的括号当然意味着我只是将第一个和第二个等于其他东西,而不是将它与“实际 cl

回答by Mike Williamson

This is a very easy answer, surprised you never got a response! :(

这是一个非常简单的答案,很惊讶您从未收到回复!:(

You have not actually created an objectyet.

您实际上还没有创建对象

For instance, you would want to write:

例如,你想写:

first = classname()

instead of just

而不仅仅是

first = classname

At the moment, how you wrote it, firstis pointing to a class. E.g., if you ask what firstis, you'd get:

目前,您如何编写它,first指向一个class。例如,如果你问是什么first,你会得到:

<class '__main__.classname'>

However, after instantiating it (by simply adding the ()at the end), you'd see that firstis now:

但是,在实例化它之后(通过简单地()在末尾添加),您first现在会看到:

<__main__.classname object at 0x101cfa3c8>

The important distinction here is that yourcall created a class, whereas mine created an object.

这里的重要区别是您的调用创建了一个,而我的调用创建了一个对象

A class is to an object as humankind is to you, or as canine is to Lassie.

类之于物体,就像人类之于你,或者狗之于 Lassie。

You created "canine", whereas you wanted to create "Lassie".

你创造了“犬”,而你想创造“Lassie”。



Note: you also usually want to initialize your objects. For that, you place an __init__method in your class.

注意:您通常还想初始化您的对象。为此,您__init__在类中放置了一个方法。