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
imgse
V2EX  ›  Python

大家帮看看问题在哪

  •  
  •   imgse · 2019-03-08 12:59:13 +08:00 · 2091 次点击
    这是一个创建于 1868 天前的主题,其中的信息可能已经有所发展或是发生改变。
    大家帮我看看,这个啥错误。
    def ss():
    global t
    print(t)
    t=True

    t=False
    ss()

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

    提示未声明,但一开始用 global 声明了。运行起来也没问题,大家帮我看看问题在哪?
    13 条回复    2019-03-12 16:55:07 +08:00
    aiver
        1
    aiver  
       2019-03-08 13:17:29 +08:00
    t = False 语句放到函数 ss 定义的前面
    wallriding
        2
    wallriding  
       2019-03-08 13:18:49 +08:00
    assignment 的意思不是“声明”,是“赋值”
    ballshapesdsd
        3
    ballshapesdsd  
       2019-03-08 13:20:20 +08:00
    可能 vscode 还没有那么智能吧,智能检测到在 ss 函数里没有对 t 赋值
    imgse
        4
    imgse  
    OP
       2019-03-08 13:34:11 +08:00
    @wallriding 写错了
    imgse
        5
    imgse  
    OP
       2019-03-08 13:35:46 +08:00
    @aiver 这样写也不算错啊,我感觉是 vscode 的 linter 有错误。
    Johnson66
        6
    Johnson66  
       2019-03-08 13:36:34 +08:00
    你这个格式~~!!
    gstqc
        7
    gstqc  
       2019-03-08 13:38:30 +08:00 via Android
    你们没缩进也能看得懂,好厉害!!!
    catalina
        8
    catalina  
       2019-03-08 13:42:10 +08:00 via Android
    @gstqc 长一点就看不懂了(笑)
    imgse
        9
    imgse  
    OP
       2019-03-08 14:03:05 +08:00
    @gstqc 编辑了好几次,一发布就把空格给我取消了。
    jingxyy
        10
    jingxyy  
       2019-03-08 14:57:28 +08:00
    不用管它,py 这种语言 linter 能发挥的功能很有限,写几个 meta class 的它就看不懂了。
    就 lz 的这个例子,也不一定说你写的这个就完全没问题,万一
    t = False
    #...(好多 code)...
    del t
    #...(好多 code)...
    #...(好多 code,然后你忘了你已经 del 了)...
    ss()
    你猜结果会是什么样?
    antipro
        11
    antipro  
       2019-03-08 15:33:27 +08:00
    现在明白大括号的用处了吧。
    wuyifar
        12
    wuyifar  
       2019-03-08 18:24:56 +08:00
    一样的代码,一样是 VScode 直接运行没有报错
    ashCloud
        13
    ashCloud  
       2019-03-12 16:55:07 +08:00
    你的声明现在函数里了,函数是后调用的。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1564 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 16:59 · PVG 00:59 · LAX 09:59 · JFK 12:59
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.