gett error message: "TypeError: 'int' object is not callable"

Dave Angel davea at
Thu Jul 9 10:52:14 EDT 2009

Nick wrote:
> I've seen a lot of posts on this problem, but none seems to help.
> Here is the code:
> /code
> file = open(prefix1)
> text = file.readlines()
> len = len(text)
> fields = text[1].split()
> num_rows = int(fields[1])
> num_cols = int(fields[2])
> U1_matrix = []
> print fields
> print repr(fields)
> print len(fields)
> for line in text[2: num_rows+2]:
>     fields = line.split()
> #    print "fields", fields, line
>     for i in range(len(fields)):
>         fields[i] = float(fields[i])
>     U1_matrix.append(fields)
> /*code
> prefix is a space/line delimited ascii file that represents a 2D
> matrix.  i'm trying to read in 2 matrices from different files, strip
> away the header stuff and then take the dot product of the 2
> matrices.  any help is much appreciated.
> thanks,
> nick
You have at least two problems with that code, one of which is causing 
your symptom.

Both 'file' and 'len' are defined in the standard library, and shouldn't 
be redefined in your code.  And your problem is that after you redefined 
'len', you then tried to use it in its original meaning.

Rename those two and you'll get further.

And it would have saved lots of time for lots of people if you included 
sample data and the actual error message, marking where in your code it 
occurs.  Once you know it's complaining about the len() call, it's not 
too hard to figure out that the problem was you rebound the len 
attribute from a function to an integer.

Traceback (most recent call last):
  File "M:\Programming\Python\sources\dummy\", line 21, in <module>
    print len(fields)
TypeError: 'int' object is not callable


More information about the Python-list mailing list