0

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?

3
  • I am such a fool. Thanks for the quick responses. Commented Oct 25, 2013 at 18:45
  • It's exactly the silly mistakes like this that are impossible to spot yourself and easy for others to spot. Commented Oct 25, 2013 at 18:54
  • I appreciate that you gave this question a title that is easy to search for so that future users will be able to reference this question. Commented Oct 25, 2013 at 20:02

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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.

2 Comments

It's not a string, it's a float (time() returns a number, and he's adding 0.4 to it—plus, look at the exception text). But otherwise, nice explanation.
@abarnert - Whoa, that was dumb. Fixed it. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.