UnboundLocalError:赋值前引用了局部变量“url_request" [英] UnboundLocalError: local variable 'url_request' referenced before assignment

查看:41
本文介绍了UnboundLocalError:赋值前引用了局部变量“url_request"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

觉得我在这里发疯了.

url_request = 0

def somefunction():
    url_request+=1

if __name__ =='__main__':
    somefunction()

给我 UnboundLocalError.我在这里遗漏了什么重要概念?

Gives me the UnboundLocalError. What important concept am I missing here?

推荐答案

您正在分配给一个全局变量,这意味着您需要将它标记为一个全局变量:

You are assigning to a global variable, which means you need to mark it as a global:

def somefunction():
    global url_request
    url_request+=1

当您在局部作用域中分配变量时,除非您先使用 global 语句告诉 python 否则,否则假定它是局部变量.

When you assign to a variable in a local scope, it is assumed to be a local variable unless you use a global statement to tell python otherwise first.

这篇关于UnboundLocalError:赋值前引用了局部变量“url_request"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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