绑定方法错误 [英] Bound method error

查看:61
本文介绍了绑定方法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个单词解析类,并在运行时不断得到
< main .Word_Parser实例的绑定方法Word_Parser.sort_word_list在0x1037dd3b0>
错误这:

I am creating a word parsing class and keep getting a "bound method Word_Parser.sort_word_list of <main.Word_Parser instance at 0x1037dd3b0>" error when I run this:

class Word_Parser:
    """docstring for Word_Parser"""
    def __init__(self, sentences):
        self.sentences = sentences

    def parser(self):
        self.word_list = self.sentences.split()

    def sort_word_list(self):
        self.sorted_word_list = self.word_list.sort()

    def num_words(self):
        self.num_words = len(self.word_list)

test = Word_Parser("mary had a little lamb")
test.parser()
test.sort_word_list()
test.num_words()
print test.word_list
print test.sort_word_list
print test.num_words


推荐答案

这里没有错误。您正在打印一个函数,这就是函数的样子。

There's no error here. You're printing a function, and that's what functions look like.

要真正调用该函数,必须在其后加上括号。您已经在上面进行了此操作。如果要打印调用函数的结果,只需让函数返回该值,然后将打印内容放在该位置即可。例如:

To actually call the function, you have to put parens after that. You're already doing that above. If you want to print the result of calling the function, just have the function return the value, and put the print there. For example:

print test.sort_word_list()

另一方面,如果您希望函数改变对象的状态,然后以其他方式打印状态,那也可以。

On the other hand, if you want the function to mutate the object's state, and then print the state some other way, that's fine too.

现在,您的代码似乎在某些地方有效,但在其他地方却无效;让我们看看原因:

Now, your code seems to work in some places, but not others; let's look at why:


  • parser 设置一个名为 word_list ,然后您随后打印test.word_list ,这样就可以使用。

  • sort_word_list 设置了一个名为 sorted_word_list 的变量,随后您打印test.sort_word_list —即功能,而不是变量。因此,您将看到绑定方法。 (此外,正如乔恩·克莱门茨(Jon Clements)指出的那样,即使您解决了这个问题,您也要打印,因为这就是 sort 返回。)

  • num_words 设置一个名为 num_words 的变量,然后再次打印该函数,但是在这种情况下,该变量与该函数具有相同的名称,这意味着您实际上是用其输出替换该函数,因此它可以正常工作。但是,这可能不是您想要的。

  • parser sets a variable called word_list, and you later print test.word_list, so that works.
  • sort_word_list sets a variable called sorted_word_list, and you later print test.sort_word_list—that is, the function, not the variable. So, you see the bound method. (Also, as Jon Clements points out, even if you fix this, you're going to print None, because that's what sort returns.)
  • num_words sets a variable called num_words, and you again print the function—but in this case, the variable has the same name as the function, meaning that you're actually replacing the function with its output, so it works. This is probably not what you want to do, however.

(在某些情况下,乍看起来是个好主意-您只想计算一次,然后一次又一次地访问它,而无需不断地重新计算它。但这不是这样做的方法。要么使用 @property ,或使用备忘录装饰器。)

(There are cases where, at first glance, that seems like it might be a good idea—you only want to compute something once, and then access it over and over again without constantly recomputing that. But this isn't the way to do it. Either use a @property, or use a memoization decorator.)

这篇关于绑定方法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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