Object Not Callable, float?

Ben Finney ben+python at benfinney.id.au
Sun Nov 29 21:48:44 EST 2009


"W. eWatson" <wolftracks 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.

-- 
 \       “To have the choice between proprietary software packages, is |
  `\      being able to choose your master. Freedom means not having a |
_o__)                        master.” —Richard M. Stallman, 2007-05-16 |
Ben Finney



More information about the Python-list mailing list