为什么在分配函数中增加变量时,Python会在分配之前抱怨引用? [英] Why does Python complain about reference before assignment when increasing variables in a function?

查看:161
本文介绍了为什么在分配函数中增加变量时,Python会在分配之前抱怨引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么Python会在分配前引用 chrome 被引用?它不会抱怨字典。这是与Python 2.5,如果它有所作为。

  def f():
google ['browser'] ='chrome'
chrome + = 1

google = dict()
chrome = 1
f()

当然,我可以使用全局chrome ,但我想知道为什么Python不考虑要分配的变量。感谢。

/python-functions-assignments-and-scope.htmlrel =nofollow>阅读这里


Why does Python complain about chrome being referenced before assignment? It does not complain about the dictionary. This is with Python 2.5 if it makes a difference.

def f():
  google['browser'] = 'chrome'
  chrome += 1

google = dict()
chrome = 1
f()

I can make it work with global chrome of course, but I'd like to know why Python does't consider the variable to be assigned. Thanks.

解决方案

It's out of scope: read here

这篇关于为什么在分配函数中增加变量时,Python会在分配之前抱怨引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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