【问题标题】:python err: Local variable referenced before assignmentpython err:赋值前引用的局部变量
【发布时间】:2013-08-17 07:20:06
【问题描述】:

我收到错误UnboundLocal: Local variable T referenced before assignment,但不是这样的:

import ...
T = 0

def do_something():
    do_something_else(T) # err at this line
T += 1

def do_something_else(t):
    print t

do_something()

这就是我的代码的外观,因此在分配之前它不是参考。 (如果我错了,请纠正我)怎么了?

【问题讨论】:

  • 我没有收到任何错误
  • 你在用ipython吗

标签: python error-handling compiler-errors


【解决方案1】:

声明T为全局变量:

def do_something():
    global T # <--------------
    do_something_else(T) # err at this line
    T += 1

【讨论】:

    猜你喜欢
    • 2021-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-14
    • 2018-06-29
    • 1970-01-01
    • 1970-01-01
    • 2013-08-02
    相关资源
    最近更新 更多