TypeError: ‘int’ object is not callable

by Jouni Miettunen


Warning: ksort() expects parameter 1 to be array, object given in /home/croozeus/public_html/blogs/wp-content/plugins/yet-another-related-posts-plugin/includes.php on line 256

Last night I spent a lot of time debugging this mysterious error note. Handling application settings for Unity game was now isolated into a new class, it initialized ok, but crashed at exit. The error note didn’t help at all, searching google didn’t help, rewriting everything for 10 times didn’t help. Frustrating!

It was one of those moments, when “just doing it” wasn’t enough. As a weekend coder I’ve never actually studied python. There’s never time and it’s much more fun to code. Now it was time to start reading the manuals, sample codes, language grammar and syntax – well after midnight 🙂


class MyClass(object):
--def __init__(self):
----self.value = 0

--def value(self, a_value=-1):
----if a_value >= 0:
------self.value = a_value
----return self.value

my_class = MyClass()
my_class.value(1)

The reason for error is that class objects and methods reside inside same namespace. Since I had both “def value(self)” and “self.value”, there was no way to know which one I was trying use, when I wrote “my_class.value(1)”. It should be obvious that it’s method, since there’s parenthesis, but python doesn’t work that way.

Anyway quick fix, inspired by numerous sample code, is to start names of class attributes with underscore. From “self.value” to “self._value”. Yes, I could change attribute names, but I’m a big fan of Natural Naming and Self Documenting Code.

Enjoy,

–jouni

Related posts:

  1. FlagIcon48 v1.00 FlagIcon48 is an example how to use IconDrawer flag 48x48...
  2. Featured PyS60 Application #4 Every 10 days, we would be featuring in the...
  3. Font Test v1.20 This is sample code for PyS60 (at least v1.4.5), presenting...
  4. Localization sample code One feature, which well-done applications have, is localization. It sets...
  5. Touch Stick 1.00 (PyS60 sample code) Touch Stick demonstrates how to code Touch UI device's...

Related posts brought to you by Yet Another Related Posts Plugin.