缺少1个必需的位置参数 [英] Missing 1 required positional argument

查看:335
本文介绍了缺少1个必需的位置参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先让我开始编程时,我会一直保持绿色。但是,我仍然需要完全理解为什么以及发生了什么。

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.

(我使用Python版本3.4.1)

(I use Python Version 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:

该错误告诉我,我需要在名称中再加上1个参数,因此我在这里肯定出错了,但是我已经尝试过以下操作:

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:

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

名称中还有更多参数吗?如果我正确理解了这个参数->()<-

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

我还排除了以下事实:它是def createname(self,name ),因为自我是或应该是孤独的,不包括在内?因此,我不太了解发生了什么。

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.

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

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

已解决:

 first = classname;
 second = classname;

应该是:

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

缺少括号的地方当然意味着我只是将第一个和第二个EQUAL设置为其他东西而未链接它与实际cl

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

推荐答案

这是一个非常简单的答案,很惊讶您没有得到响应!:(

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

您尚未真正创建对象

例如,您想写入:

first = classname()

而不是

first = classname

此刻,您如何编写它,第一指向。例如,如果您问第一个是什么,您会得到:

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

<class '__main__.classname'>

但是,实例化之后(只需在末尾添加()),您会看到 first 现在是:

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

<__main__.classname object at 0x101cfa3c8>

此处的重要区别是您的调用创建了 class ,而我的创建了 object

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


一个类对一个对象就像人类对您一样,对犬一样对Lassie一样。

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".

注意:您通常还想初始化对象。为此,您可以在类中放置 __ init __ 方法。

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

这篇关于缺少1个必需的位置参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆