h h h h - 1 month ago 8
Python Question

'int' object is not callable error in class

In python 2.7, I am writing a class called

, which is to act as a counter for very large integers. I believe I have it riddled out, but I keep running into
TypeError: 'int' object is not callable
, which seems to mean that at some point in my code I tried to call an
like it was a function. Many of the examples I found on this site were simply a mathematical error where the writer omitted an operator. I can't seem to find my error.

Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
TypeError: 'int' object is not callable

My code:

class Zillion:
def __init__(self, digits):
self.new = []
self.count = 0 # for use in process and increment
self.increment = 1 # for use in increment
def process(self, digits):
if digits == '':
raise RuntimeError
elif digits[0].isdigit() == False:
if digits[0] == ' ' or digits[0] == ',':
digits = digits[1:]
raise RuntimeError
elif digits[0].isdigit():
digits = digits[1:]
self.count += 1
if digits != '':
process(self, digits)
process(self, digits)
if self.count == 0:
raise RuntimeError

self.new2 = self.new # for use in isZero

def toString(self):
self.mystring =''
self.x = 0
while self.x < self.count:
self.mystring = self.mystring + str(self.new[self.x])
self.x += 1

def isZero(self):
if self.new2[0] != '0':
return False
elif self.new2[0] == '0':
self.new2 = self.new2[1:]
return True

def increment(self):
if self.new[self.count - self.increment] == 9:
self.new[self.count - self.increment] = 0
if isZero(self):
self.count += 1
self.new= [1] + self.new
self.increment += 1
elif self.new[self.count - self.increment] != 9:
self.new[self.count - self.increment] = self.new[self.count - self.increment] + 1


You have both an instance variable and a method named increment that seems to be your problem with that traceback at least.

in __init__ you define self.increment = 1 and that masks the method with the same name

To fix, just rename one of them (and if it's the variable name, make sure you change all the places that use it--like throughout the increment method)

One way to see what's happening here is to use type to investigate. For example:

>>> type(Zillion.increment)
<type 'instancemethod'>
>>> z = Zillion('5')
>>> type(z.incremenet)
<type 'int'>