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

请教,如何解决 yield from 报错

  •  
  •   louzhumuyou · 2017-08-08 09:06:49 +08:00 · 4006 次点击
    这是一个创建于 2425 天前的主题,其中的信息可能已经有所发展或是发生改变。

    遇到一个问题,我再看书的过程中,看到 yield from 语法,想尝试一下,然后就输入了书上的例子,然后编译器提示 yield from 语法报错了:

    from collections import Iterable
    
    def flatten(items, ignore_types=(str, bytes)):
        for x in items:
            if isinstance(x, Iterable) and not isinstance(x, ignore_types):
                yield from flatten(x)
            else:
                yield x
    

    报错提示是 yield 语法有问题:

    yield from flatten(x)
           ^
    SyntaxError: invalid syntax
    

    经过一番 google,有人提出升级 setuptools 能解决,可是我执行了 pip install --upgrade setuptools pip 升级之后,依然有这样的报错提示。我的环境是 mac,pyhon 2.7,这里的问题该如何解决呢?

    17 条回复    2017-08-11 14:00:31 +08:00
    rocksolid
        1
    rocksolid  
       2017-08-08 09:11:53 +08:00   ❤️ 1
    3 的语法放在 pyhon 2.7 里用了
    Lycnir
        2
    Lycnir  
       2017-08-08 09:12:02 +08:00
    请看 python3
    louzhumuyou
        3
    louzhumuyou  
    OP
       2017-08-08 09:17:02 +08:00
    @rocksolid 能在 2 里面调用 python3 的语法吗?我记得原来好像看到过。不知道有没有记错。
    rocksolid
        4
    rocksolid  
       2017-08-08 09:23:53 +08:00
    @louzhumuyou
    from __future__ import xxxxxxx
    louzhumuyou
        5
    louzhumuyou  
    OP
       2017-08-08 09:24:00 +08:00
    @Lycnir 如果我想在 python 2.7 里面用,该怎么办呢?
    sujin190
        6
    sujin190  
       2017-08-08 09:27:06 +08:00
    @louzhumuyou #5 这种语法 2.7 肯定用不了啊,否则 3 和 2.7 的兼容问题不是被你解决了
    laike9m
        7
    laike9m  
       2017-08-08 09:33:09 +08:00 via Android
    yield from 是没法在 2 里用的
    ylcc
        8
    ylcc  
       2017-08-08 10:09:39 +08:00
    看到标题我就感觉是在 2 里面跑了,果然。。。
    leavic
        9
    leavic  
       2017-08-08 10:23:33 +08:00
    @louzhumuyou 自己把 python2 的源码拿来修改,自己实现 yield from 语法,然后用自己的 python2,除此,无他。
    dsg001
        10
    dsg001  
       2017-08-08 10:26:31 +08:00
    移植到 py2,只能用 for 了
    ipwx
        11
    ipwx  
       2017-08-08 10:32:21 +08:00
    Python 2 不支持 yield from。

    其实我觉得是时候投向 Python 3 了,除非你有老项目的历史包袱。

    P.S. yield from xx 等价于

    for x in xx: yield x
    dreamapple
        12
    dreamapple  
       2017-08-08 11:08:12 +08:00 via Android
    书都是 python3 的书,还用 python2 的解释器
    louzhumuyou
        13
    louzhumuyou  
    OP
       2017-08-08 13:45:46 +08:00
    @dreamapple 书上没写,没指明,一直用的 python 2.7,之前写的很多项目都是 2.7 的。在考虑中迁移。
    louzhumuyou
        14
    louzhumuyou  
    OP
       2017-08-08 13:46:00 +08:00
    @ipwx 谢谢,在考虑迁移。
    badttt
        15
    badttt  
       2017-08-08 16:04:15 +08:00
    python2.x 和 python3 是两种语言
    SoulMelody
        16
    SoulMelody  
       2017-08-09 14:30:52 +08:00
    860670496
        17
    860670496  
       2017-08-11 14:00:31 +08:00
    早日投奔 py3,不受苦
    前一阵为了给新服务器同时兼容 2 和 3 费了半天劲
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5192 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 36ms · UTC 01:20 · PVG 09:20 · LAX 18:20 · JFK 21:20
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.