Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

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

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


nleioatt at gmail

Jul 9, 2009, 6:42 AM

Post #1 of 20 (1230 views)
Permalink
gett error message: "TypeError: 'int' object is not callable"

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
--
http://mail.python.org/mailman/listinfo/python-list


lutz.horn at fastmail

Jul 9, 2009, 6:50 AM

Post #2 of 20 (1211 views)
Permalink
Re: gett error message: "TypeError: 'int' object is not callable" [In reply to]

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
--
http://mail.python.org/mailman/listinfo/python-list


fridrik at pyth

Jul 9, 2009, 6:53 AM

Post #3 of 20 (1210 views)
Permalink
Re: gett error message: "TypeError: 'int' object is not callable" [In reply to]

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.
--
http://mail.python.org/mailman/listinfo/python-list


R.Brodie at rl

Jul 9, 2009, 7:02 AM

Post #4 of 20 (1208 views)
Permalink
Re: gett error message: "TypeError: 'int' object is not callable" [In reply to]

"Nick" <nleioatt [at] gmail> wrote in message
news:e54c4461-c0b7-42fb-8542-cefd7bf5f89f [at] h18g2000yqj

> 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


nleioatt at gmail

Jul 9, 2009, 7:08 AM

Post #5 of 20 (1209 views)
Permalink
Re: gett error message: "TypeError: 'int' object is not callable" [In reply to]

On Jul 9, 10:02 am, "Richard Brodie" <R.Bro...@rl.ac.uk> wrote:
> "Nick" <nleio...@gmail.com> wrote in message
>
> news:e54c4461-c0b7-42fb-8542-cefd7bf5f89f [at] h18g2000yqj
>
> > 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
--
http://mail.python.org/mailman/listinfo/python-list


bruno.42.desthuilliers at websiteburo

Jul 9, 2009, 7:24 AM

Post #6 of 20 (1215 views)
Permalink
Re: gett error message: "TypeError: 'int' object is not callable" [In reply to]

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
--
http://mail.python.org/mailman/listinfo/python-list


davea at ieee

Jul 9, 2009, 7:52 AM

Post #7 of 20 (1215 views)
Permalink
Re: gett error message: "TypeError: 'int' object is not callable" [In reply to]

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
--
http://mail.python.org/mailman/listinfo/python-list


fridrik at pyth

Jul 9, 2009, 8:30 AM

Post #8 of 20 (1215 views)
Permalink
Re: gett error message: "TypeError: 'int' object is not callable" [In reply to]

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
--
http://mail.python.org/mailman/listinfo/python-list


nleioatt at gmail

Jul 9, 2009, 9:04 AM

Post #9 of 20 (1218 views)
Permalink
Re: gett error message: "TypeError: 'int' object is not callable" [In reply to]

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 <frid...@pyth.net> 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

--
http://mail.python.org/mailman/listinfo/python-list


tkermode at gmail

Jul 9, 2009, 9:06 AM

Post #10 of 20 (1207 views)
Permalink
Re: gett error message: "TypeError: 'int' object is not callable" [In reply to]

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 <R.Brodie [at] rl>:
>
> "Nick" <nleioatt [at] gmail> wrote in message
> news:e54c4461-c0b7-42fb-8542-cefd7bf5f89f [at] h18g2000yqj
>
>> 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
--
http://mail.python.org/mailman/listinfo/python-list


R.Brodie at rl

Jul 9, 2009, 9:30 AM

Post #11 of 20 (1207 views)
Permalink
Re: gett error message: "TypeError: 'int' object is not callable" [In reply to]

"Tom Kermode" <tkermode [at] gmail> wrote in message
news:mailman.2903.1247155607.8015.python-list [at] python

> 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.

No, but not redefining the ones you actually use is a good start.
Learning to understand the traceback is the more important lesson,
IMHO. It takes a while to tune into what error messages are trying
to tell you; even when you stop making newbie mistakes, you're
going to have to deal with runtime errors from time to time.


--
http://mail.python.org/mailman/listinfo/python-list


fridrik at pyth

Jul 9, 2009, 9:40 AM

Post #12 of 20 (1208 views)
Permalink
Re: gett error message: "TypeError: 'int' object is not callable" [In reply to]

Tom Kermode wrote:
> 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.

One way is using a code checker like PyChecker[1]. This neat software
for finding bugs will check for lots of other pitfalls too, but you
can filter it down to what you need if you're only interested in this
one.

I don't use an IDE, but this would seem like something for an IDE[2]
to support if you're into that kind of magic.

Regards,
Friðrik Már

[1] http://pychecker.sourceforge.net/
[2] http://wiki.python.org/moin/IntegratedDevelopmentEnvironments
--
http://mail.python.org/mailman/listinfo/python-list


rhodri at wildebst

Jul 9, 2009, 10:41 AM

Post #13 of 20 (1194 views)
Permalink
Re: gett error message: "TypeError: 'int' object is not callable" [In reply to]

On Thu, 09 Jul 2009 17:06:45 +0100, Tom Kermode <tkermode [at] gmail> wrote:

> 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.

It's only really a pitfall if you try to use the built-in after you've
redefined it. That's the thing to keep an eye open for.

--
Rhodri James *-* Wildebeest Herder to the Masses
--
http://mail.python.org/mailman/listinfo/python-list


fridrik at pyth

Jul 9, 2009, 11:57 AM

Post #14 of 20 (1201 views)
Permalink
Re: gett error message: "TypeError: 'int' object is not callable" [In reply to]

Hi Rhodri,

> It's only really a pitfall if you try to use the built-in after you've
> redefined it. That's the thing to keep an eye open for.

You're right, but in cases where you're editing a codebase which you
didn't author entirely by yourself you may not be aware of that.

That said, if the codebase you're working on is structured (short,
concise methods) you should be working with small, consumable scopes
you can inhale in entirety before modifying.

Regards,
Friðrik Már
--
http://mail.python.org/mailman/listinfo/python-list


sajmikins at gmail

Jul 9, 2009, 5:01 PM

Post #15 of 20 (1192 views)
Permalink
Re: gett error message: "TypeError: 'int' object is not callable" [In reply to]

On Thu, Jul 9, 2009 at 9:42 AM, Nick<nleioatt [at] gmail> wrote:
<snip>
>    fields = line.split()
>    for i in range(len(fields)):
>        fields[i] = float(fields[i])


instead of the above code you could say:

fields = [float(n) for n in in line.split()]

Have fun getting back into python! :] (A lot has changed in the last few years)
HTH,
~Simon
--
http://mail.python.org/mailman/listinfo/python-list


http://phr.cx at NOSPAM

Jul 9, 2009, 5:22 PM

Post #16 of 20 (1195 views)
Permalink
Re: gett error message: "TypeError: 'int' object is not callable" [In reply to]

Nick <nleioatt [at] gmail> writes:
> text = file.readlines()
> len = len(text)
> fields = text[1].split()

Is that intended to split the first line of the file? Remember
that arrays in python begin at index 0.
--
http://mail.python.org/mailman/listinfo/python-list


lie.1296 at gmail

Jul 10, 2009, 1:03 AM

Post #17 of 20 (1192 views)
Permalink
Re: gett error message: "TypeError: 'int' object is not callable" [In reply to]

Friðrik Már Jónsson wrote:
> Hi Rhodri,
>
>> It's only really a pitfall if you try to use the built-in after you've
>> redefined it. That's the thing to keep an eye open for.
>
> You're right, but in cases where you're editing a codebase which you
> didn't author entirely by yourself you may not be aware of that.
>
> That said, if the codebase you're working on is structured (short,
> concise methods) you should be working with small, consumable scopes you
> can inhale in entirety before modifying.
>
> Regards,
> Friðrik Már

But if you are responsible for a large codebase that you don't write
yourself, it is doubtful that you're a real newbie.
--
http://mail.python.org/mailman/listinfo/python-list


lie.1296 at gmail

Jul 10, 2009, 1:47 AM

Post #18 of 20 (1191 views)
Permalink
Re: gett error message: "TypeError: 'int' object is not callable" [In reply to]

Simon Forman wrote:
> On Thu, Jul 9, 2009 at 9:42 AM, Nick<nleioatt [at] gmail> wrote:
> <snip>
>> fields = line.split()
>> for i in range(len(fields)):
>> fields[i] = float(fields[i])
>
>
> instead of the above code you could say:
>
> fields = [float(n) for n in in line.split()]
>
> Have fun getting back into python! :] (A lot has changed in the last few years)

>>> fields = [float(n) for n in in line.split()]
File "<stdin>", line 1
fields = [float(n) for n in in line.split()]
^
SyntaxError: invalid syntax

s/in in/in/
--
http://mail.python.org/mailman/listinfo/python-list


nleioatt at gmail

Jul 10, 2009, 10:26 AM

Post #19 of 20 (1188 views)
Permalink
Re: gett error message: "TypeError: 'int' object is not callable" [In reply to]

On Jul 9, 8:22 pm, Paul Rubin <http://phr...@NOSPAM.invalid> wrote:
> Nick <nleio...@gmail.com> writes:
> > text = file.readlines()
> > len = len(text)
> > fields = text[1].split()
>
> Is that intended to split the first line of the file? Remember
> that arrays in python begin at index 0.

no the '1st line' is garbled meta data, but thanks man
--
http://mail.python.org/mailman/listinfo/python-list


jcd at sdf

Jul 10, 2009, 11:59 AM

Post #20 of 20 (1190 views)
Permalink
Re: gett error message: "TypeError: 'int' object is not callable" [In reply to]

On Thu, 2009-07-09 at 13:53 +0000, Friðrik Már Jónsson wrote:
> 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.

While we're on the subject of good question posting form: The body of
your post should contain all relevant information. Please don't make
readers look back at the subject heading for the statement of the
problem. Duplicating the statement of the problem in the subject and
the body is ok, but it really ought to be in the body.

--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.