Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > gett error message: "TypeError: 'int' object is not callable"

Reply
Thread Tools

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

 
 
Nick
Guest
Posts: n/a
 
      07-09-2009
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
 
Reply With Quote
 
 
 
 
Lutz Horn
Guest
Posts: n/a
 
      07-09-2009
Hi,

Nick schrieb:
> I've seen a lot of posts on this problem, but none seems to help.


Could you please post a sample input file and the exact error message?

Thanks
Lutz
--
Strike Out ⇒ http://www.fourmilab.ch/documents/strikeout
 
Reply With Quote
 
 
 
 
Friðrik Már Jónsson
Guest
Posts: n/a
 
      07-09-2009
Look at:

len = len(text)

You're overriding `len` (a built-in method), with an integer
(`len(text)`). You then call:

for i in range(len(fields)):

But `len` is no longer a callable, but merely an integer.

Regards,
Friðrik Már

P.S. While this is a fairly obvious problem it's usually a good idea
to post working code and a traceback when requesting help.
 
Reply With Quote
 
Richard Brodie
Guest
Posts: n/a
 
      07-09-2009

"Nick" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

> file = open(prefix1)
> text = file.readlines()
> len = len(text)


You have redefined two built-in functions "file" and "len" in the first three lines.
This is usually considered poor practice. Stick to meaningless variable names,
it's safer (only joking).

TypeError: 'int' object is not callable". This means that something you thought
was a function is in fact an integer. It's helpful to post/look at the line number of
the error; "how is this line failing", is much easier to answer than
"how is my program failing".

print len(fields)

Here len is an integer, because you redefined it in line 3. I'm guessing this is the
problem.


 
Reply With Quote
 
Nick
Guest
Posts: n/a
 
      07-09-2009
On Jul 9, 10:02 am, "Richard Brodie" <(E-Mail Removed)> wrote:
> "Nick" <(E-Mail Removed)> wrote in message
>
> news:(E-Mail Removed)...
>
> > file = open(prefix1)
> > text = file.readlines()
> > len = len(text)

>
> You have redefined two built-in functions "file" and "len" in the first three lines.
> This is usually considered poor practice. Stick to meaningless variable names,
> it's safer (only joking).
>
> TypeError: 'int' object is not callable". This means that something you thought
> was a function is in fact an integer. It's helpful to post/look at the line number of
> the error; "how is this line failing", is much easier to answer than
> "how is my program failing".
>
> print len(fields)
>
> Here len is an integer, because you redefined it in line 3. I'm guessing this is the
> problem.


thanks for spotting the obvious errors, its my 2nd day programming
python in about 3 years.
fridrick, code should be workable with the exception of the
errors...thats the whole program

Thanks again for all the help problem fixed
 
Reply With Quote
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      07-09-2009
Nick a écrit :
> I've seen a lot of posts on this problem, but none seems to help.
> Here is the code:
> /code
>
> file = open(prefix1)


shadows the builtin 'file' type.


> text = file.readlines()
> len = len(text)


shadows the builtin 'len' function.

> fields = text[1].split()
> num_rows = int(fields[1])
> num_cols = int(fields[2])
>
> U1_matrix = []
>
> print fields
> print repr(fields)
> print len(fields)


And here's your problem - 'len' is now bound to the result of the
previous call to len(text).

Hint : Python's functions, classes and modules are objects too, and
don't live in a distinct namespace. So _don't_ use builtin's types /
functions / etc names as identifiers.

HTH
 
Reply With Quote
 
Dave Angel
Guest
Posts: n/a
 
      07-09-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\echo2.py" , line 21, in <module>
print len(fields)
TypeError: 'int' object is not callable


DaveA
 
Reply With Quote
 
Friðrik Már Jónsson
Guest
Posts: n/a
 
      07-09-2009
Previously, I wrote:
>> P.S. While this is a fairly obvious problem it's usually a good
>> idea to post working code and a traceback when requesting help.


Nick wrote:
> thanks for spotting the obvious errors, its my 2nd day programming
> python in about 3 years.


I'm sorry, my saying it was obvious may have come off a little
arrogant. My clumsily delivered point was that because it was a small
snippet of code it didn't take much time to run through for someone
who codes daily with Python. What you did there was a perfectly
ordinary thing to do until experience teaches you how Python does
things.

> fridrick, code should be workable with the exception of the
> errors...thats the whole program


You're right, I failed to say it explicitely but I was referring to
the input file. In some cases, albeit not this one, problems can
exist in parsing gotchas.

Regards,
Friðrik Már
 
Reply With Quote
 
Nick
Guest
Posts: n/a
 
      07-09-2009
no problem, i understand, i haven't coded anything in literally 2
years, but it was a simple and pretty obvious mistake. thanks for all
your help,

nick


On Jul 9, 11:30 am, Friðrik Már Jónsson <(E-Mail Removed)> wrote:
> Previously, I wrote:
> >> P.S. While this is a fairly obvious problem it's usually a good
> >> idea to post working code and a traceback when requesting help.

> Nick wrote:
> > thanks for spotting the obvious errors, its my 2nd day programming
> > python in about 3 years.

>
> I'm sorry, my saying it was obvious may have come off a little
> arrogant. My clumsily delivered point was that because it was a small
> snippet of code it didn't take much time to run through for someone
> who codes daily with Python. What you did there was a perfectly
> ordinary thing to do until experience teaches you how Python does
> things.
>
> > fridrick, code should be workable with the exception of the
> > errors...thats the whole program

>
> You're right, I failed to say it explicitely but I was referring to
> the input file. In some cases, albeit not this one, problems can
> exist in parsing gotchas.
>
> Regards,
> Friðrik Már


 
Reply With Quote
 
Tom Kermode
Guest
Posts: n/a
 
      07-09-2009
Hi,

Do you know a good way to avoid running into this problem? It
makes sense to suggest not calling variables the same names as
built-in functions, but that's hard for a new python programmer who
doesn't already know what all the built-in functions are. Over time a
programmer will learn which names to avoid, but it's a bit of a
pitfall early on.

Cheers,
Tom



2009/7/9 Richard Brodie <(E-Mail Removed)>:
>
> "Nick" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>
>> file = open(prefix1)
>> text = file.readlines()
>> len = len(text)

>
> You have redefined two built-in functions "file" and "len" in the first three lines.
> This is usually considered poor practice. Stick to meaningless variable names,
> it's safer (only joking).
>
> TypeError: 'int' object is not callable". This means that something you thought
> was a function is in fact an integer. It's helpful to post/look at the line number of
> the error; "how is this line failing", is much easier to answer than
> "how is my program failing".
>
> print len(fields)
>
> Here len is an integer, because you redefined it in line 3. I'm guessing this is the
> problem.
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>




--
http://www.kiloday.com
http://www.fourstopspast.com
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[compilation error] debugging this error message "Object() in java.lang.Object cannot be applied to" Gavin Java 3 03-28-2007 05:57 AM
Object creation - Do we really need to create a parent for a derieved object - can't the base object just point to an already created base object jon wayne C++ 9 09-22-2005 02:06 AM
Error:Object reference not set to an instance of an object. Suresh Kojhani ASP .Net 1 07-29-2004 12:10 PM
Error !Object reference not set to an instance of an object. !!! Help Parthiv Joshi ASP .Net 2 07-02-2004 10:28 AM
HELP! Error Loading ASPX : Object Reference not set to an instance object Pedro Correia ASP .Net 0 07-25-2003 10:42 AM



Advertisments