TypeError: ‘int’ object is not callable
by Jouni Miettunen
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:
- FlagIcon48 v1.00 FlagIcon48 is an example how to use IconDrawer flag 48x48...
- Featured PyS60 Application #4 Every 10 days, we would be featuring in the...
- Font Test v1.20 This is sample code for PyS60 (at least v1.4.5), presenting...
- Localization sample code One feature, which well-done applications have, is localization. It sets...
- 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.
It becomes more apparent that doing that is an error when you have ‘bound methods’ for callbacks.
It’s just one of those things you do once and remember forever!
here u define same name for the class attribute and the class function
another istake is that a_value is not defined
this is the refined program
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()
print my_class.value(1)
@Mike: Exactly! Since I couldn’t find much help via google, I decided to “document” it – just in case I forget 🙂
@Mahendra: sorry about tipo! I spend lots of time trying to figure out how to write code into WordPress and at some point I corrupted the code. Thanx for fix!
–jouni