繁体   English   中英

烧瓶中出现奇怪的错误:

[英]Strange error in flask:

以下代码(根据第一个答案对此进行了编辑):

time_difference_in_seconds = int(
    (datetime.combine(date.today(), max_time)
     - datetime.combine(date.today(), min_time)
).total_seconds())
[...]
for date in dates:
[...]

给我这个错误:

UnboundLocalError: local variable 'date' referenced before assignment

为了清楚起见,我从datetime导入了date和datetime。 而且,当我在werkzeug调试器中执行代码时,它工作得很好,并给了我想要的结果:

[console ready]
>>> int((datetime.combine(date.today(), max_time)
        - datetime.combine(date.today(), min_time)).total_seconds())
46800

实际上,我在不同的视图中使用了相同的函数,并且在该视图中不会引起任何错误。

是什么导致此奇怪的错误,我该如何解决?

您稍后会在函数中为date分配一些值,这会导致Python将date视为对局部变量的引用,而不是您期望的导入模块

在函数中重命名此date变量可解决问题。

我想您必须在函数中分配一些date ,例如:

def somefunc():
    time_difference_in_seconds = int(
        (datetime.combine(date.today(), max_time)
         - datetime.combine(date.today(), min_time)
    ).total_seconds())
    date = ...

重命名变量将解决此问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM