V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
smdbh
V2EX  ›  Python

变量作用域问题, shadow name x from outer scope

  •  
  •   smdbh · 2018-07-04 10:26:22 +08:00 · 4787 次点击
    这是一个创建于 2094 天前的主题,其中的信息可能已经有所发展或是发生改变。
    一个字符串拼接函数
    def func(ver):
    __ret = ''
    __for k, v in ver.items():
    ____ret += '{0}:{1}'.format(k, v)
    __return ret

    问题是第二行的 ret='' 回报 shadow name ret from outer scope 的错误。要再 for 循环里加 global ret 才能去掉。
    我想问下,这类代码,常规做法是怎么做的?
    第 1 条附言  ·  2018-07-04 15:04:06 +08:00
    在 __main__ 中的变量和函数里的一致,会造成这个警告。命名要不一样。
    3 条回复    2018-07-04 11:01:30 +08:00
    whoami9894
        1
    whoami9894  
       2018-07-04 10:47:46 +08:00 via Android
    你在函数外部定义了同名变量
    scmod
        2
    scmod  
       2018-07-04 10:59:58 +08:00
    这个是错误吗~?我感觉这种是警告是不是会比较好..
    chenqh
        3
    chenqh  
       2018-07-04 11:01:30 +08:00
    这个不是 pycharm 的警告吗?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1262 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 21ms · UTC 17:54 · PVG 01:54 · LAX 10:54 · JFK 13:54
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.