课堂上的自我要求论证

发布于 2024-12-10 09:55:53 字数 633 浏览 0 评论 0原文

由于某种原因,大多数类实例都返回类型错误,指出传递的参数不足,问题出在 self 上。 这工作得很好:

class ExampleClass:
    def __init__(self, some_message):
        self.message = some_message
        print ("New ExampleClass instance created, with message:")
        print (self.message)
ex = ExampleClass("message")

但是,我定义和调用实例的几乎所有其他类都会返回相同的错误。几乎相同的函数:

class Test(object):
    def __init__(self):
        self.defaultmsg = "message"
    def say(self):
        print(self.defaultmsg)
test = Test
test.say()

返回类型错误,表示它需要一个参数。我不仅在该类中遇到这个问题,而且在我定义的几乎每个类中都遇到这个问题,而且我不知道问题是什么。我刚刚更新了 python,但之前遇到了错误。我对编程相当陌生。

For some reason most instances of classes are returning Type errors saying that insufficient arguments were passed, the problem is with self.
This works fine:

class ExampleClass:
    def __init__(self, some_message):
        self.message = some_message
        print ("New ExampleClass instance created, with message:")
        print (self.message)
ex = ExampleClass("message")

However almost every other Class I define and call an instance of returns the same error. The almost identical function:

class Test(object):
    def __init__(self):
        self.defaultmsg = "message"
    def say(self):
        print(self.defaultmsg)
test = Test
test.say()

Returns a Type Error, saying that it needs an argument. I'm getting this problem not just with that class, but with pretty much every class I define, and I have no idea what the problem is. I just updated python, but was getting the error before. I'm fairly new to programming.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

老旧海报 2024-12-17 09:55:53

您必须实例化该类:

test = Test()   #test is an instance of class Test

如果

test = Test     #test is the class Test
test.say()      #TypeError: unbound method say() must be called with Test instance as first argum ent (got nothing instead)

您好奇,您可以尝试以下操作:

test = Test
test.say(Test())     #same as Test.say(Test()) 

它有效,因为我将类实例(自身)赋予了未绑定方法!
绝对不建议以这种方式编码。

You have to instantiate the class:

test = Test()   #test is an instance of class Test

instead of

test = Test     #test is the class Test
test.say()      #TypeError: unbound method say() must be called with Test instance as first argum ent (got nothing instead)

if you are curious you can try this:

test = Test
test.say(Test())     #same as Test.say(Test()) 

It works because I gave the class instance (self) to the unbound method !
Absolutely not recommended to code this way.

献世佛 2024-12-17 09:55:53

您应该添加括号来实例化一个类:

test = Test()

You should add parentheses to instantiate a class:

test = Test()
溺孤伤于心 2024-12-17 09:55:53

您的测试引用类本身,而不是该类的实例。要创建实际的测试实例或“实例化”它,请添加括号。例如:

>>> class Foo(object):
...   pass
... 
>>> Foo
<class '__main__.Foo'>
>>> Foo()
<__main__.Foo object at 0xa2eb50>

错误消息试图告诉您没有这样的 self 对象可以(隐式)作为函数参数传递,因为在您的情况下 test.say将是一个未绑定的方法。

Your test refers to the class itself, rather than an instance of that class. To create an actual test instance, or to 'instantiate' it, add the parentheses. For example:

>>> class Foo(object):
...   pass
... 
>>> Foo
<class '__main__.Foo'>
>>> Foo()
<__main__.Foo object at 0xa2eb50>

The error message was trying to tell you that there was no such self object to pass in (implicitly) as the function argument, because in your case test.say would be an unbound method.

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