WhiteHotLoveTiger
WhiteHotLoveTiger

Reputation: 2228

Python TypeError float is not callable

Here is a short program which is giving me an error I'm having a difficult time understanding:

import time
TIMEOUT_LENGTH = 0.4
TIMEOUT_CHECK = False
STOPPED = True
timeout = 0.0

def start_timer():
    global timeout
    global STOPPED
    global TIMEOUT_CHECK

    TIMEOUT_CHECK = False
    STOPPED = False
    timeout = time.time() + TIMEOUT_LENGTH

def stop_timer():
    global STOPPED
    global TIMEOUT_CHECK

    TIMEOUT_CHECK = False
    STOPPED = True

def timeout():   
    global timeout
    global STOPPED
    global TIMEOUT_CHECK

    currTime = time.time()
    if (currTime > timeout) and (STOPPED == False):
       TIMEOUT_CHECK = True
    return TIMEOUT_CHECK

start_timer()
print timeout()

Running this gives me:

Traceback (most recent call last):
  File "prob.py", line 34, in <module>
    print timeout()
TypeError: 'float' object is not callable

It doesn't look to me as if I'm trying to call currTime or timeout. What is going on here that I'm not understanding?

Upvotes: 0

Views: 4477

Answers (2)

user2555451
user2555451

Reputation:

You make a function named timeout, but then you override that and make timeout a float here:

timeout = time.time() + TIMEOUT_LENGTH

You need to either change the name of the function or the name of the float. They both can't be named timeout.

Upvotes: 2

Martijn Pieters
Martijn Pieters

Reputation: 1121346

You cannot have both a function and another variable named timeout. Rename one or the other.

As it stands, you first bind timeout to a float value 0.0. You then rebind it by defining a timeout() function. Last but not least, by calling start_timer() you rebind timeout again, back to a float number:

By the time you try to execute print timeout(), timeout is bound to a floating point value and not a function anymore.

Upvotes: 4

Related Questions