推荐学习书目
› 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
imgse
V2EX  ›  Python

大家帮看看问题在哪

  •  
  •   imgse · Mar 8, 2019 · 3455 views
    This topic created in 2803 days ago, the information mentioned may be changed or developed.
    大家帮我看看,这个啥错误。
    def ss():
    global t
    print(t)
    t=True

    t=False
    ss()

    在 vscode 里面一直报错:
    Using variable 't' before assignment

    提示未声明,但一开始用 global 声明了。运行起来也没问题,大家帮我看看问题在哪?
    13 replies  •  2019-03-12 16:55:07 +08:00
    aiver
    aiver
    Mar 8, 2019
    1
    t = False 语句放到函数 ss 定义的前面
    wallriding
    wallriding
    Mar 8, 2019
    2
    assignment 的意思不是“声明”,是“赋值”
    ballshapesdsd
    ballshapesdsd
    Mar 8, 2019
    3
    可能 vscode 还没有那么智能吧,智能检测到在 ss 函数里没有对 t 赋值
    imgse
    imgse
    OP
    Mar 8, 2019
    4
    @wallriding 写错了
    imgse
    imgse
    OP
    Mar 8, 2019
    5
    @aiver 这样写也不算错啊,我感觉是 vscode 的 linter 有错误。
    Johnson66
    Johnson66
    Mar 8, 2019
    6
    你这个格式~~!!
    0ZXYDDu796nVCFxq
    0ZXYDDu796nVCFxq
    Mar 8, 2019 via Android
    7
    你们没缩进也能看得懂,好厉害!!!
    catalina
    catalina
    Mar 8, 2019 via Android
    8
    @gstqc 长一点就看不懂了(笑)
    imgse
    imgse
    OP
    Mar 8, 2019
    9
    @gstqc 编辑了好几次,一发布就把空格给我取消了。
    jingxyy
    jingxyy
    Mar 8, 2019
    10
    不用管它,py 这种语言 linter 能发挥的功能很有限,写几个 meta class 的它就看不懂了。
    就 lz 的这个例子,也不一定说你写的这个就完全没问题,万一
    t = False
    #...(好多 code)...
    del t
    #...(好多 code)...
    #...(好多 code,然后你忘了你已经 del 了)...
    ss()
    你猜结果会是什么样?
    antipro
    antipro
    Mar 8, 2019
    11
    现在明白大括号的用处了吧。
    wuyifar
    wuyifar
    Mar 8, 2019
    12
    一样的代码,一样是 VScode 直接运行没有报错
    ashCloud
    ashCloud
    Mar 12, 2019
    13
    你的声明现在函数里了,函数是后调用的。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2043 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 38ms · UTC 14:55 · PVG 22:55 · LAX 06:55 · JFK 09:55
    ♥ Do have faith in what you're doing.