Python:为什么导入时间不适用于 time() 但适用于 time.sleep()? [英] Python: Why does import time not work for time() but works for time.sleep()?

查看:78
本文介绍了Python:为什么导入时间不适用于 time() 但适用于 time.sleep()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我使用 from time import time,Python 2.7.3 无法识别 time.sleep(60).但是如果我使用import time,那么Python 就不能识别t=time().为什么会发生这种情况?有什么办法可以在同一个程序中使用 time()time.sleep(x) 吗?

 from time 导入时间#导入时间间隔分钟 = 1t = 时间()而 1:时间.睡眠(60)

我得到的错误类型是:

<块引用>

回溯(最近一次调用最后一次):文件myProg.py",第 9 行,在time.sleep(60) AttributeError: 'builtin_function_or_method' 对象没有属性 'sleep'

解决方案

你需要决定你想让 time 的名称指代什么,模块或函数叫做 time在模块中.你可以写:

<预><代码>>>>从时间导入时间,睡眠>>>时间()1347806075.148084>>>睡觉(3)>>>

<预><代码>>>>导入时间>>>时间.time()1347806085.739065>>>时间.sleep(2)>>>

If I use from time import time, the Python 2.7.3 does not recognize time.sleep(60). But if I use import time, then Python does not recognize t=time(). Why does this happen? Is there any way I can use time() and time.sleep(x) in the same program?

from time import time
#import time

intervalInMinute = 1
t = time()
while 1:
    time.sleep(60)

The kind of error I get is:

Traceback (most recent call last): File "myProg.py", line 9, in time.sleep(60) AttributeError: 'builtin_function_or_method' object has no attribute 'sleep'

解决方案

You need to decide what you want the name time to refer to, the module or the function called time in the module. You can write:

>>> from time import time, sleep
>>> time()
1347806075.148084
>>> sleep(3)
>>>

or

>>> import time 
>>> time.time()
1347806085.739065
>>> time.sleep(2)
>>>

这篇关于Python:为什么导入时间不适用于 time() 但适用于 time.sleep()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆