Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "bound method" error mean when I call a function?

Tags:

python

class

I am creating a word parsing class and I 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 
like image 772
Michael Conlin Avatar asked Oct 29 '12 22:10

Michael Conlin


People also ask

What is a bound method?

A bound method is the one which is dependent on the instance of the class as the first argument. It passes the instance as the first argument which is used to access the variables and functions. In Python 3 and newer versions of python, all functions in the class are by default bound methods.

What is bounded method call?

If a function is an attribute of class and it is accessed via the instances, they are called bound methods. A bound method is one that has ' self ' as its first argument. Since these are dependent on the instance of classes, these are also known as instance methods.

What is an unbound method call?

An unbound method is essentially a function with some trimmings. A 'bound method' is called that because the first argument (ie self ) is already set to a ; you can call b(10) and it works just the same way as if you had done a. fred(10) (this is actually necessary given how CPython operates).

What the difference between bound and unbound methods?

When a bound method is called, it calls im_func with im_self as the first parameter followed by its calling parameters. unbound methods call the underlying function with just its calling parameters. Starting with Python 3, there are no unbound methods. Class.


2 Answers

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

(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.)

like image 51
abarnert Avatar answered Sep 22 '22 06:09

abarnert


This problem happens as a result of calling a method without brackets. Take a look at the example below:

class SomeClass(object):     def __init__(self):         print 'I am starting'      def some_meth(self):         print 'I am a method()'  x = SomeClass() ''' Not adding the bracket after the method call would result in method bound error ''' print x.some_meth ''' However this is how it should be called and it does solve it ''' x.some_meth() 
like image 36
blakroku Avatar answered Sep 22 '22 06:09

blakroku