Bound Method Python

This is a mini post that only exists to hopefully help out people like me who are dumb sometimes.

If you are pulling your hair out trying to figure out why you are getting something like this:


Then you are probably trying to do something like this:

MyClass = MyClass()
print MyClass.my_method

You should be doing:

MyClass = MyClass()
print MyClass.my_method()

In the former code block, python was printing exactly what you told it to. It’s a lot like Ruby, in that everything is an object. It will happily “print” a class method for you and even give you the location in memory of it! But that’s not what you wanted. You wanted it to execute the class method.

I hope this saves someone out there some unnecessary troubleshooting.

Leave a Reply

Your email address will not be published.