【问题标题】:UnboundLocalError: local variable 'html' referenced before assignmentUnboundLocalError:分配前引用的局部变量“html”
【发布时间】:2019-03-22 21:07:52
【问题描述】:
html += '''
<table style="width:100%">
  <tr align="center">
    <th style="width:10%">Metrics</th>
    '''
def get_bus_metrics (met,name):
    for i in met:
        html += '<th>' + str(i) + '</th>'
    html += '''</tr>'''
    html += '''<tr><th>''' + name +'''</th>'''

get_bus_metrics (g1,'R')

UnboundLocalError: 赋值前引用了局部变量 'html'

我收到此错误。 有人可以建议我在这里缺少什么,为什么会出现上述错误。

【问题讨论】:

  • 你的意思是在第一行使用 += 吗?如果尚未分配 html 并且您尝试将其添加到其中,那么这就是您的错误来自的地方
  • 您正在为 python 2.7 和 3.6 创建 crioss-workable 代码?如果没有,请仅标记您使用的版本。
  • @strava Html 在&lt;table&gt; 标签之前需要一个&lt;html&gt;&lt;body&gt; 标签才有意义。第 1 行可能是复制和粘贴问题。问题更可能是代码在全局范围内使用 html 并尝试在 get_bus_metric() 函数中修改它而不将其声明为全局或将其作为参数提供 - += 也命中
  • @PatrickArtner 我刚刚回复了您的代码,请您看一下。我无法将函数输出到 html 代码

标签: python html python-3.x python-2.7


【解决方案1】:

如果变量之前没有使用过,则修复+=并将其提供给函数:

# fix here - no += unless you decleared html as some string beforehand
html = '''
<table style="width:100%">
  <tr align="center">
    <th style="width:10%">Metrics</th>
    '''
# fix here - else not known
def get_bus_metrics (met,name,html):
    for i in met:
        html += '<th>' + str(i) + '</th>'
    html += '''</tr>'''
    html += '''<tr><th>''' + name +'''</th>'''
    return html

html = get_bus_metrics (range(1,5),'R',html)  # provide as parameter: cleaner

print(html) # 

输出:

<table style="width:100%">
  <tr align="center">
    <th style="width:10%">Metrics</th>
    <th>1</th><th>2</th><th>3</th><th>4</th></tr><tr><th>R</th>

或(不太可取)将其声明为全局:

def get_bus_metrics (met,name,html):
    # don't use globals, they are _evil_ - if you need to _modify_ smth
    # from global scope, you need to announce this in the function
    # reading works without if decleard earlier then the function, 
    # changing it needs this line:
    global html   
    for i in met:
        html += '<th>' + str(i) + '</th>'
    html += '''</tr>'''
    html += '''<tr><th>''' + name +'''</th>'''

提示 1
使用str.format()f-strings / PEP-0498 / Literal String Interpolation 更好地格式化字符串

提示 2
在循环中添加字符串是浪费的——它构造了许多被丢弃的中间字符串。改用列表

def get_bus_metrics (met,name,html):
    t = []
    for i in met:
        t.append('<th>{}</th>'.format(i))  # using format(..)
    t.append(f'</tr><tr><th>{name}</th>')  # using string interpol
    return html+''.join(t)                 # faster, less wasted intermediate strings

独库:

【讨论】:

  • def get_bus_metrics(value,name,html_table,sym=None): for i in value: lol.append('{:,.0f}'.format(i)) html_table += '' ' '''+name+'''''' for i in lol: if sym is not None: html_table += '' + '$' + str(i) + '' else: html_table += '' + ' ' + str(i) + '' html_table += '''''' get_bus_metrics(g2,' Row',html_table,1) 嘿,Patrick 有没有想过我在这里缺少什么,Row 没有在 HTML 代码中添加
  • @EricDsilva 注释不适用于代码。您的函数使用 lol 而不先定义它 - 在顶部尝试 lol = []。 cmets 中缺少整个缩进。您不会从函数返回构造的字符串。如果您有其他问题,请发布第二个问题(如果它不是真实的) - 否则编辑并添加到这个问题。不要在一个问题中问不同的东西,因为这会使答案的可重用性变得更糟(即要求苹果,得到苹果的答案,再次要求南瓜 - 人们在南瓜后搜索得到很多苹果的答案 - 诸如此类)。
【解决方案2】:

html+= someValhtml = html + someVal 相同。

在变量html 未定义之前尚未初始化。

由于html 未定义,您无法执行html + someVal

【讨论】:

    猜你喜欢
    • 2017-08-10
    • 2020-01-16
    • 2019-12-05
    • 2017-09-19
    • 2020-09-26
    • 2022-01-02
    • 2019-01-24
    • 2021-07-01
    • 2021-10-16
    相关资源
    最近更新 更多