What Does "Bound Method" Error Mean When I Call a Function

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

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

getting bound method error when calling from class in python

You are missing the () to actually call the method

print("Pet's age is: " + str (item.get_pet_age))

Should be

print("Pet's age is: " + str (item.get_pet_age()))

python bound method Error

You could use a return value in the perimeter function:

def perimeter(self):
return self.length*2 + self.width*2

Then call A.perimeter() instead of A.perimeter.

print "This is the perimeter: ", A.perimeter()

Likewise for Area.

def Area(self):
return self.length*self.width

print "This is the area: ", A.Area()

EDIT: Sorry, I rushed my answer and didn't bother checking it. Here is a working replacement for the Rectangle class and PrintALL() function. I also edited above.

It is better if you pass number types (not strings) to the function and you can avoid rounding errors by using floats instead of integers.

class Rectangle(TwoDShapes):
def __init__(self, nameOfShape, length, width, numberofSides, numberOfVertices):
self.nameOfShape = "Rectangle"
self.length = length
self.width = width
self.numberofSides = 4
self.numberOfVertices = 4
super(Rectangle,self).__init__()

def perimeter(self):
return self.length*2.0 + self.width*2.0
def Area(self):
return self.length*self.width

def PrintALL():

A = Rectangle("Rectangle", 10.0, 20.0, 4.0, 4.0)

print "This is the name of the shape: ", A.nameOfShape
print "This is the length: ", A.length
print "This is the width: ", A.width
print "This is the number of side: ", A.numberofSides
print "This is the number of vertice: ", A.numberOfVertices
print "This is the perimeter: ", A.perimeter()
print "This is the area: ", A.Area()
print "\n"
PrintALL()

OUTPUT:

This is the name of the shape:  Rectangle
This is the length: 10.0
This is the width: 20.0
This is the number of side: 4
This is the number of vertice: 4
This is the perimeter: 60.0
This is the area: 200.0

class returns bound method ... instead of the value I returned (python)

This is a very common mistake in python, and sadly easy to reproduce and fall for it, but once you do it, you will never do it again:

env = Environment()
nodes = env.setEnv

that is not the same as:

env = Environment()
nodes = env.setEnv()

With () , you are invoking a function in python, without them, you are just returning the reference to the function, but not the "execution of the function".

What does bound method str.native JS of 's' mean?

change it to

gm = gm.lower()

gm.lower is the method itself not the resulting string of the lower method of the str



Related Topics



Leave a reply



Submit