推荐学习书目
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 · Jul 4, 2018 · 5550 views
    This topic created in 2893 days ago, the information mentioned may be changed or developed.
    一个字符串拼接函数
    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 才能去掉。
    我想问下,这类代码,常规做法是怎么做的?
    Supplement 1  ·  Jul 4, 2018
    在 __main__ 中的变量和函数里的一致,会造成这个警告。命名要不一样。
    3 replies    2018-07-04 11:01:30 +08:00
    whoami9894
        1
    whoami9894  
       Jul 4, 2018 via Android
    你在函数外部定义了同名变量
    scmod
        2
    scmod  
       Jul 4, 2018
    这个是错误吗~?我感觉这种是警告是不是会比较好..
    simple2025
        3
    simple2025  
       Jul 4, 2018
    这个不是 pycharm 的警告吗?
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   4034 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 05:16 · PVG 13:16 · LAX 22:16 · JFK 01:16
    ♥ Do have faith in what you're doing.