推荐学习书目
› 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 · Aug 8, 2017 · 5027 views
    This topic created in 3392 days ago, the information mentioned may be changed or developed.

    遇到一个问题,我再看书的过程中,看到 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 replies  •  2017-08-11 14:00:31 +08:00
    rocksolid
    rocksolid
    Aug 8, 2017❤️1
    1
    3 的语法放在 pyhon 2.7 里用了
    Lycnir
    Lycnir
    Aug 8, 2017
    2
    请看 python3
    louzhumuyou
    louzhumuyou
    OP
    Aug 8, 2017
    3
    @rocksolid 能在 2 里面调用 python3 的语法吗?我记得原来好像看到过。不知道有没有记错。
    rocksolid
    rocksolid
    Aug 8, 2017
    4
    @louzhumuyou
    from __future__ import xxxxxxx
    louzhumuyou
    louzhumuyou
    OP
    Aug 8, 2017
    5
    @Lycnir 如果我想在 python 2.7 里面用,该怎么办呢?
    sujin190
    sujin190
    Aug 8, 2017
    6
    @louzhumuyou #5 这种语法 2.7 肯定用不了啊,否则 3 和 2.7 的兼容问题不是被你解决了
    laike9m
    laike9m
    Aug 8, 2017 via Android
    7
    yield from 是没法在 2 里用的
    ylcc
    ylcc
    Aug 8, 2017
    8
    看到标题我就感觉是在 2 里面跑了,果然。。。
    leavic
    leavic
    Aug 8, 2017
    9
    @louzhumuyou 自己把 python2 的源码拿来修改,自己实现 yield from 语法,然后用自己的 python2,除此,无他。
    dsg001
    dsg001
    Aug 8, 2017
    10
    移植到 py2,只能用 for 了
    ipwx
    ipwx
    Aug 8, 2017
    11
    Python 2 不支持 yield from。

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

    P.S. yield from xx 等价于

    for x in xx: yield x
    dreamapple
    dreamapple
    Aug 8, 2017 via Android
    12
    书都是 python3 的书,还用 python2 的解释器
    louzhumuyou
    louzhumuyou
    OP
    Aug 8, 2017
    13
    @dreamapple 书上没写,没指明,一直用的 python 2.7,之前写的很多项目都是 2.7 的。在考虑中迁移。
    louzhumuyou
    louzhumuyou
    OP
    Aug 8, 2017
    14
    @ipwx 谢谢,在考虑迁移。
    badttt
    badttt
    Aug 8, 2017
    15
    python2.x 和 python3 是两种语言
    860670496
    860670496
    Aug 11, 2017
    17
    早日投奔 py3,不受苦
    前一阵为了给新服务器同时兼容 2 和 3 费了半天劲
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1809 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 45ms · UTC 10:27 · PVG 18:27 · LAX 02:27 · JFK 05:27
    ♥ Do have faith in what you're doing.