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

求助 Python 语法问题

  •  1
     
  •   tuobashao · 13 天前 · 1572 次点击
    在 shell 中执行
    python -c "if 1==1: print('121')"
    正常打印 121

    但是执行
    python -c "1==1;if 1==1: print('121')"
    就报错
    1==1;if 1==1: print('121')
    ^^
    SyntaxError: invalid syntax

    这里的分号;我感觉是换行的意思,为什么会报错,求大佬帮忙解答下,thanks
    第 1 条附言  ·  12 天前
    感谢俩位大佬 @nagisaushio,@zictos

    第一次了解到 python 的简单语句概念,也第一次知道 exec 函数可以直接带换行执行,thanks
    16 条回复    2024-09-09 08:35:05 +08:00
    ZZ74
        1
    ZZ74  
       13 天前 via Android   ❤️ 1
    python 啥时候用分号换行了
    不都是靠空格和游标卡尺么
    tuobashao
        2
    tuobashao  
    OP
       13 天前
    @ZZ74 脚本里面是这样,
    但是用 shell 调用,之前我都是当分号是换行的,比如下面这句执行就正常打印 1 出来
    python -c "a=1;b=2;print(a)"
    zictos
        3
    zictos  
       13 天前
    说明包含了 if 就不能用分号了,你直接写在 python 代码中执行也会报错。
    你可以用 base64 编码后再解码并用 exec 执行
    ZZ74
        4
    ZZ74  
       13 天前 via Android
    @tuobashao 不怎么懂 python 但是 a=1 是有效的赋值语句 但是单纯一个 1==1 常见语言都会报错
    zictos
        5
    zictos  
       13 天前
    @zictos #3
    python -c"import base64; exec(base64.b64decode('CjE9PTEKaWYgMT09MToKICAgIHByaW50KCcxMjEnKQo=').decode())"
    nagisaushio
        6
    nagisaushio  
       13 天前 via Android   ❤️ 3
    楼上没说到点上

    https://docs.python.org/3/reference/compound_stmts.html#grammar-token-python-grammar-statement

    python 中有个 simple_stmt 的概念,只有 simple_stmt 才能用分号隔开,但 if 不是
    nagisaushio
        7
    nagisaushio  
       13 天前 via Android
    @ZZ74

    > 但是单纯一个 1==1 常见语言都会报错

    试了一下,不是那么常见。java go 会报错,js python php c c++ 不会
    tuobashao
        8
    tuobashao  
    OP
       13 天前
    @nagisaushio 感谢大佬,👍
    zictos
        9
    zictos  
       13 天前
    @zictos #3 刚又试了下,其实用 exec 后不用 base64 也行,用\n 换行,把所有的换行和缩进都模拟真实的多行代码。
    python -c "exec('''1 == 1\nif 1 == 1:\n print('121')''')"
    nagisaushio
        10
    nagisaushio  
       13 天前 via Android
    @zictos 这绕到不知道什么地方去了。Bash 的单引号本来就支持多行字符串,再不济也可以用 heredoc 也就是<<EOF 这种,或者 prtintf ... | python ,完全没必要绕
    zictos
        11
    zictos  
       13 天前
    @nagisaushio #10 只是写成一行的方法,楼主似乎还是很想写成一行的,不然他可能都不会提这个问题。
    prtintf ... | python 其实也并没有简单到哪里去,跟我的例子也差不了太多,我确实一开始不知道这种用法。
    hutoer
        12
    hutoer  
       13 天前
    当 if 前面有语句时,格式是:语句 if 条件 else 语句。

    这样就可以了:
    python -c "1==1; print('121') if 1==1 else print()"
    iorilu
        13
    iorilu  
       13 天前
    为什么有这种需求呢, 从 bash 执行字符串代码?
    zictos
        14
    zictos  
       13 天前   ❤️ 1
    @hutoer #12 这其实只是简单例子,实际情况可能更复杂,比如有 for 循环或 while 循环或 if 嵌套。
    也可以平时简单代码用分号,在遇到 if 等情况时就用 exec 。
    python -c "1 == 1; exec('''if 1 == 1:\n print('121')'''); 2 == 2"
    tuobashao
        15
    tuobashao  
    OP
       12 天前
    @zictos 感谢👍
    noahlias
        16
    noahlias  
       7 天前
    这是要把 python 当 bash 使用吗哈哈 总觉得这么执行怪怪的
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   988 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 19:21 · PVG 03:21 · LAX 12:21 · JFK 15:21
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.