Object Not Callable, float?

W

W. eWatson

Here's an traceback error msg I get.

Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python25\lib\lib-tk\Tkinter.py", line 1403, in __call__
return self.func(*args)
File
"C:\Sandia_Meteors\Sentinel_Development\Development_Sentuser+Utilities\sentuser\sentuser_20090103+hist.py",
line 467, in ShowHistogram
mean = sum(hist)
TypeError: 'float' object is not callable

for the code:
----------------------
sum = 0.0
avg = 0.0
nplt_bins = 32
for i in range(len(hist)):
# msg = "%5d %6d\n" % (i,hist)
msg = "%5d %6d\n" % (i,hist)
sum = sum + hist
text.insert( END, msg )
for i in range(len(hist)):
avg = avg + (i*hist/sum)

mean = sum(hist) <-------------- faulty line
mean = mean/256.0
-------------------------end

hist is a list of 256 integers. What does float have to do with this?
 
W

W. eWatson

Ben said:
W. eWatson said:
"C:\Sandia_Meteors\Sentinel_Development\Development_Sentuser+Utilities\sentuser\sentuser_20090103+hist.py",
line 467, in ShowHistogram
mean = sum(hist)
TypeError: 'float' object is not callable

It means you're calling an object of type ‘float’. The line where it
occurred shows that you're accessing that object through the name ‘sum’,
which means you've bound the name ‘sum’ to a float object.
for the code:

Here you clobber the existing binding of ‘sum’, binding it to the float
value 0.0.
avg = 0.0
nplt_bins = 32
for i in range(len(hist)):
# msg = "%5d %6d\n" % (i,hist)
msg = "%5d %6d\n" % (i,hist)
sum = sum + hist


Here you keep re-binding the name ‘sum’ to new float objects of
different value.
text.insert( END, msg )
for i in range(len(hist)):
avg = avg + (i*hist/sum)

mean = sum(hist) <-------------- faulty line


Here you try to call the object referenced by the name ‘sum’, which is a
float object.
hist is a list of 256 integers. What does float have to do with this?

You explicitly bound the name ‘sum’ to an object of type ‘float’.

Solution: Choose names wisely, and if you want to use a built-in name
like ‘sum’ for its built-in putpose, don't clobber that binding before
using it.

Yikes. Thanks very much. Python seems to act unlike other language in
which words like float are reserved. I'll use asum.
 
C

Carl Banks

It means you're calling an object of type ‘float’. The line where it
occurred shows that you're accessing that object through the name ‘sum’,
which means you've bound the name ‘sum’ to a float object.
Here you clobber the existing binding of ‘sum’, binding it to the float
value 0.0.
        avg = 0.0
        nplt_bins = 32
        for i in range(len(hist)):
#             msg = "%5d %6d\n" % (i,hist)
            msg = "%5d %6d\n" % (i,hist)
            sum = sum + hist

Here you keep re-binding the name ‘sum’ to new float objects of
different value.
            text.insert( END, msg )
        for i in range(len(hist)):
            avg = avg + (i*hist/sum)
        mean = sum(hist)   <-------------- faulty line

Here you try to call the object referenced by the name ‘sum’, which is a
float object.
You explicitly bound the name ‘sum’ to an object of type ‘float’.
Solution: Choose names wisely, and if you want to use a built-in name
like ‘sum’ for its built-in putpose, don't clobber that binding before
using it.

Yikes. Thanks very much. Python seems to act unlike other language in
which words like float are reserved. I'll use asum.


That "float" isn't reserved isn't the problem here since the conflict
occurred with the word sum, which is a function. Most languages I
know don't reserve the names of functions. For instance you can't do
this in C:

int printf = 1;
printf("%d\n", printf);

Python doesn't reserve the names of types either, which is a little
uncommon (but not unheard of), so that can be a gotcha.


Carl Banks
 
J

John Bokma

W. eWatson said:
Yikes. Thanks very much. Python seems to act unlike other language in
which words like float are reserved. I'll use asum.

The problem is that there is a function sum and you creating a float sum:

sum = 0.0

and

mean = sum(hist)

even if both could exist side by side it would be very confusing IMO.

John
 
W

W. eWatson

John said:
The problem is that there is a function sum and you creating a float sum:

sum = 0.0

and

mean = sum(hist)

even if both could exist side by side it would be very confusing IMO.

John
I think I understand it, but how does one prevent it from happening, or
know it's the cause? That msg I got?

I think PL/I, FORTRAN, ALGOL, etc. have reserved words.
 
L

Lie Ryan

I think I understand it, but how does one prevent it from happening, or
know it's the cause? That msg I got?

I think PL/I, FORTRAN, ALGOL, etc. have reserved words.

Generally, use PyLint or similar tools; they'll warn you.
 
M

Mel

W. eWatson said:
I think PL/I, FORTRAN, ALGOL, etc. have reserved words.

Algol reserved syntactic tokens that resembled English words, but specified
that they should be written in a different way from programmer-defined
symbols, so no conflict was possible. Published algorithms might have the
tokens underlined or boldfaced. In the Algol-60 I used, the tokens had to
be enclosed in apostrophes. None of this applied to library subroutine
names; it was open season on those.

In FORTRAN and PL/I words were un-reserved to a degree that's really
bizarre. A short post can't begin to do it justice -- let's just mention
that IF and THEN could be variable names, and DO 100 I=1.10 . The syntaxes
were carefully crafted so that context completely determined whether a
symbol would be taken in a reserved sense or a programmer-defined sense, so
any possibility for conflict was a syntax error.

Mel.
 
N

Nobody

In FORTRAN and PL/I words were un-reserved to a degree that's really
bizarre. A short post can't begin to do it justice -- let's just mention
that IF and THEN could be variable names, and DO 100 I=1.10 . The syntaxes
were carefully crafted so that context completely determined whether a
symbol would be taken in a reserved sense or a programmer-defined sense, so
any possibility for conflict was a syntax error.

And then Lisp and Tcl just don't have reserved words. Many symbols are
pre-defined, but you're free to re-define them (Lisp won't let you use
'quote' as a function name, but you can still use it as a variable name).
 
T

Terry Reedy

W. eWatson said:
I think I understand it, but how does one prevent it from happening, or
know it's the cause? That msg I got?

Yes. The message 'x is not callable', where x is a name of something you
expect to be callable (such as the builtin functions and classes),
signals that x has been rebound to a non-callable object.

On the other hand, if x is something you know not to be callable, there
there is either a typo -- yxz(3) instead of yzx[3] -- or a bug such as
expr(arg) where you expect expr to evaluate to a function but it does
not. Say yzx[3]('abc') where you think yxz is a list of functions, or
yzx(3)('abc') where you expect yzx to return a function.

tjr
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,699
Latest member
AnneRosen

Latest Threads

Top