Object Not Callable, float?

Carl Banks pavlovevidence at gmail.com
Mon Nov 30 05:23:22 CET 2009


On Nov 29, 8:14 pm, "W. eWatson" <wolftra... at invalid.com> wrote:
> Ben Finney wrote:
> > "W. eWatson" <wolftra... at invalid.com> writes:
>
> >> "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:
> >> ----------------------
> >>         sum = 0.0
>
> > 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[i])
> >>             msg = "%5d %6d\n" % (i,hist[i])
> >>             sum = sum + hist[i]
>
> > 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[i]/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.

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



More information about the Python-list mailing list