0

I have a small python program written for python 2.7.3:

import time
def fun():
    print('Hi')
for i in range(3):
    Timer(i, fun).start()

When I run it, I get the error:

NameError: name 'Timer' is not defined

How can I find out which module supports this functionality?

3
  • You're looking for timeit module, aren't ya? Commented Jun 13, 2014 at 17:20
  • Try from timeit import Timer. Commented Jun 13, 2014 at 17:21
  • 1
    I've edited your question to pull out the opinion based question on Python 2 vs 3. Commented Jun 13, 2014 at 17:22

2 Answers 2

5

Timer is in the timeit module, not time. And to call it like you want to, you would have to from timeit import Timer, not just import timeit. If you just declare import timeit, then you would have to write timeit.Timer instead of Timer everywhere in the code.

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

3 Comments

Thanks, but i get the error : ValueError: stmt is neither a string nor callable
You need lambda function - stackoverflow.com/questions/7523767/…
Then I get the error : AttributeError: 'Timer' object has no attribute 'start'
0

You are looking for the timeit module. You can use your existing code by replacing your current import:

from timeit import Timer

Comments

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.