This is my personal blog. I also have a professional blog at http://siliconsparrow.com/

16 September 2009

Python function is not callable

Filed under: Nerd Notes — adam @ 8:39 pm

Here’s a Python gotcha that got me. Here is my function to print out a range of sinusoidal values:

import math

def cycle(centre, range):
        theta = 4.71
        speed = 0.1
        for i in range(0,80):
                theta = theta + speed
                value = centre + (range * math.sin(theta))
                print value

I got the following error which confused the hell out of me:

Traceback (most recent call last):
  File "cycle2.py", line 11, in 
    cycle(0.0,10.0)
  File "cycle2.py", line 6, in cycle
    for i in range(0,80):
TypeError: 'float' object is not callable

Can you see it? It took me a while. I’d assigned a variable called range and then tried to call the range() function. I’d overloaded a built-in function. Duh!

I just had to rename my variable and it was good.

3 Comments »

  1. Comment by SWrelic — 24 May 2010 @ 11:06 pm

    Have you stopped using a keyword as your variable name?

    http://docs.python.org/library/functions.html?highlight=range#range

  2. Comment by Krista Grothoff — 5 October 2010 @ 6:03 pm

    Ah, thank you. I was doing the same idiot thing with min, and the error message is cryptic (well, unless you see the error – then it makes sense). D’oh!

    Anyway, you saved me some time. :)

  3. Comment by Antonio — 20 April 2011 @ 4:42 pm

    Haha… I just spend a good 10 minutes trying to figure out the same error, have to be more careful when naming variables. Thanks for your post.

RSS feed for comments on this post.

Leave a comment

COMMENTS ARE DISABLED DUE TO EXCESSIVE SPAM. I'm sorry about this, I really love to read your comments but the amount of time I spend deleting spam is too much.


Powered by WordPress