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

协程概念里的 yield,翻译成中文,最合适的解释是什么?

  •  1
     
  •   zlel · Apr 13, 2018 · 16225 views
    This topic created in 3126 days ago, the information mentioned may be changed or developed.

    词典对 yield 的解释有:屈服;出产,产生;放弃。 我怎么感觉这几个意思放在 yield 的用法上都说的通,最准确的解释是哪种?

    102 replies  •  2018-04-15 22:29:13 +08:00
    1  2  
    ❮ ❯
    guangwong
    guangwong
    Apr 13, 2018❤️1
    1
    退位
    InternetExplorer
    InternetExplorer
    Apr 13, 2018❤️1
    2
    甩出
    zj299792458
    zj299792458
    Apr 13, 2018 via iPhone
    3
    投降
    crossoverJie
    crossoverJie
    Apr 13, 2018
    4
    求饶?
    starmoon1994
    starmoon1994
    Apr 13, 2018❤️3
    5
    算是一种避让吧
    类似于开车的时候遇到了 120 急救车,需要先让急救车通过,你才能拿继续开
    fixmestan
    fixmestan
    Apr 13, 2018 via iPad
    6
    让出
    LinusKou
    LinusKou
    Apr 13, 2018
    7
    最准确的当然是不翻啊,翻过来就没意思了。
    est
    est
    Apr 13, 2018
    8
    叫唤。
    leriou
    leriou
    Apr 13, 2018❤️1
    9
    出让
    padeoe
    padeoe
    Apr 13, 2018
    10
    难道不是 “生成 /生产” 吗?
    wlwood
    wlwood
    Apr 13, 2018
    11
    就是 yield. 中文意思还是 yield.
    HanningWu
    HanningWu
    Apr 13, 2018
    12
    让行
    crb912
    crb912
    Apr 13, 2018 via Android
    13
    看中文解释是不地道的。
    if one thing yields to another, it is replaced by that thing。也就是:让位于,被…所替代。

    所有协程的不同线程关系是同等的。屈服?就有点存在优先级的味道了。它不是子程序
    wlwood
    wlwood
    Apr 13, 2018
    14
    其实我觉得,要是讲解给别人,不一定要给他讲这个中文意思啥。
    让他 yield 多几次,然后他就知道 yield 是啥了。😂😄
    paulzhang1992
    paulzhang1992
    Apr 13, 2018
    15
    我之前是学土木的,yield strgenth 一般就指使物体形变无法复原的力,大概感觉一下,估计翻译成屈服?
    wlwood
    wlwood
    Apr 13, 2018
    16
    @paulzhang1992 翻译没问题。但是,但是,看看屈服跟协程,会有关系么?刚开始看 yield 会更加一脸懵逼吧😂😄
    paulzhang1992
    paulzhang1992
    Apr 13, 2018
    17
    @wlwood 就是这种名词纯翻译的话应该有规范,理解的话结合实际意义更方便一些,提出 yield strength 就是觉得能有更直观的感受,cs 我也是自学刚刚入门😂
    zhang1215
    zhang1215
    Apr 13, 2018
    18
    让步
    vegito2002
    vegito2002
    Apr 13, 2018
    19
    认怂
    mokeyjay
    mokeyjay
    Apr 13, 2018
    20
    参考 PHP 官方中文文档的话,应该翻译为“生成”、“生成器”
    xzpjerry731
    xzpjerry731
    Apr 13, 2018❤️1
    21
    生产呀。yield 还有产量的意思
    neoblackcap
    neoblackcap
    Apr 13, 2018❤️3
    22
    让渡或者易位,这里我觉得意译更重要,想想鲁棒性
    chengxiao
    chengxiao
    Apr 13, 2018
    23
    走你
    newtype0092
    newtype0092
    Apr 13, 2018❤️7
    24
    你行你上
    nicevar
    nicevar
    Apr 13, 2018
    25
    我丢
    pathbox
    pathbox
    Apr 13, 2018 via iPhone
    26
    不翻译
    fogisland
    fogisland
    Apr 13, 2018
    27
    @newtype0092 哈哈哈,很妙
    zrq495
    zrq495
    Apr 13, 2018 via iPhone
    28
    禅让🙃
    xmadi
    xmadi
    Apr 13, 2018 via iPhone
    29
    生成 yield 这语法是来自生成器的 如果从协程的角度没法翻译
    ericls
    ericls
    Apr 13, 2018 via iPhone
    30
    @paulzhang1992 哈哈 我也是学土木的

    Yield 这里有两个意思 交出控制权 和产出一个值 两个意思都用上了 很妙啊
    binux
    binux
    Apr 13, 2018
    31
    让道
    在国外开车,看到 yield 就知道这个 yield 用得很形象了
    catror
    catror
    Apr 13, 2018
    32
    我休息下,你上吧
    MeteorCat
    MeteorCat
    Apr 13, 2018 via Android
    33
    让出+1
    guyskk0x0
    guyskk0x0
    Apr 13, 2018 via Android
    34
    交棒
    接力赛,把接力棒交给另一个协程
    peterontheway
    peterontheway
    Apr 13, 2018
    35
    礼让(给这个线程)
    CYKun
    CYKun
    Apr 13, 2018 via Android
    36
    看维基里关于生成器的解释:

    A generator is very similar to a function that returns an array, in that a generator has parameters, can be called, and generates a sequence of values. However, instead of building an array containing all the values and returning them all at once, a generator yields the values one at a time, which requires less memory and allows the caller to get started processing the first few values immediately.

    生成器类似于一个返回值为数组的函数;不同于普通函数直接构造并返回( return )整个数组的做法,生成器每次返回( yield )其中的一个值。

    从行文里对 yield 的用法来看,yield 其实就是返回的意思。从逻辑上看,yield 跟 return 的行为也是类似的,都是结束当前函数执行,回到调用处并提供一个返回值。

    所以 yield 的含义没有大家想的那么深奥,其实就是在 return 这个关键字被占用了的情况下,找了一个合适的近义词来表示生成器里这种特殊的“返回”而已。

    相应的,如果我们要把 yield 翻译成中文的话,那也找一个比较合适的、与返回近义的词就行。如回归、交付之类的都可以。
    northisland
    northisland
    Apr 13, 2018
    37
    虚报工作量
    northisland
    northisland
    Apr 13, 2018
    38
    抱歉,忘了不在 python 节点。
    vlike
    vlike
    Apr 13, 2018 via Android
    39
    挂起
    xpresslink
    xpresslink
    Apr 13, 2018
    40
    @northisland 主流编程语言中很多都有这个关键字,如 C#,PHP,javascript 之类,java 也有 yeild 方法。
    基本都是协程相关。
    kaedea
    kaedea
    Apr 13, 2018 via Android
    41
    认怂
    CSM
    CSM
    Apr 13, 2018 via Android
    42
    产出
    安道翻译的《流畅的 Python 》中如是翻译 yield

    只要和 return 的翻译区别开就行啦
    hheedat
    hheedat
    Apr 13, 2018
    43
    景甜
    luoway
    luoway
    Apr 13, 2018
    44
    看例句
    yield the floor to sb 让插嘴
    yield the right way to sb/sth 让行
    JamesRuan
    JamesRuan
    Apr 13, 2018
    45
    出让
    xcatliu
    xcatliu
    Apr 13, 2018
    46
    丫得
    Rufeer
    Rufeer
    Apr 13, 2018
    47
    刚好看到,《 Python 核心编程》里面是这样写的:
    线程包括开始、执行顺序和结束三部分。它有一个指令指针,用于记录当前运行的上下 文。当其他线程运行时,它可以被抢占(中断)和临时挂起(也称为睡眠)——这种做法叫 做让步(yielding)。
    geelaw
    geelaw
    Apr 13, 2018 via iPhone
    48
    “让出”

    This is the same “ yield ” as in that a thread (voluntarily) yields (the CPU).
    pigzilla
    pigzilla
    Apr 13, 2018❤️17
    49
    协程( coroutine )的协( co )是协作( cooperative )的意思,与之相对的是抢占式( preemptive )。

    抢占式,就是轮到我执行了,现在执行的代码会被强制保存上下文然后暂停执行,换我执行。然而风水轮流转,我也会被这么抢占。抢占的过程通常是 OS 管理的,所以有可能抢占的时机并不是很合适因为 OS 不知道你每个线程现在在做啥合不合适暂停,但好在可以保证所有代码都能得到执行。

    协作式,就是我执行过程中,到了一个阶段发现没事可做了(通常是等待 IO ),这时候 yield,主动把 CPU 资源让出来让别的代码去执行。同样的,别的代码也可能会在合适的时机主动暂停执行,让我再回来继续执行,如果我需要的其它资源已经到位( IO 数据已经有了)。协程的好处是这个让出的时机是开发时在代码里面明确写清楚了的( yield 关键字),所以让出的时机可以更合理。

    yield 带来两个主要结果:1 )主动暂停代码的执行,让渡自己的资源占用; 2 )已经执行的代码可能产出一个中间成果。而事实上 yield 这个词在英文的本意就正好带有这两个含义。

    因为谈到协程通常是和抢占式的多线程相比,也就是更注重第一个含义 /结果,所以 yield 在协程这个概念里面的最好翻译是“让出”。当然最好是不翻译,直接用 yield。

    说 yield 类似于 return 的,只看到了 yield 的第二个结果,忽略了第一个。
    paparika
    paparika
    Apr 13, 2018
    50
    让渡+1 说生产产出的不知道咋想的
    yoloppp
    yoloppp
    Apr 13, 2018
    51
    甩锅
    scriptB0y
    scriptB0y
    Apr 13, 2018
    52
    呐喊
    we3613040
    we3613040
    Apr 13, 2018
    53
    @pigzilla 厉害,豁然开朗
    qiyuey
    qiyuey
    Apr 13, 2018
    54
    我想到了 [套接字]
    zhwithsweet
    zhwithsweet
    Apr 13, 2018
    55
    不如叫 乐不思蜀 吧
    weizhen199
    weizhen199
    Apr 13, 2018
    56
    回避,全部回避!
    winglight2016
    winglight2016
    Apr 13, 2018
    57
    关键字还是不要翻的好,记得以前 UML 里 usecase 被翻译成用况,被人骂死~~~
    heiher
    heiher
    Apr 13, 2018
    58
    滚蛋
    kaedea
    kaedea
    Apr 13, 2018❤️4
    59
    为了表现的友好一点:禅位
    mengzhuo
    mengzhuo
    Apr 13, 2018
    60
    @pigzilla #49 还可以补充一下,一些 Python 第三方库,例如 gevent,或者 Go 语言层面都消除了 yield,但背后仍然是协作式的调度。
    wyntalgeer
    wyntalgeer
    Apr 13, 2018
    61
    备胎
    patx
    patx
    Apr 13, 2018
    62
    意译的话就是:生成迭代器。其他的什么生产、让渡,只会跟鲁棒性和套接字一样,搞得大家莫名其妙。
    glasslion
    glasslion
    Apr 13, 2018
    63
    第一感觉是 让渡
    还是不翻译为好
    neoblackcap
    neoblackcap
    Apr 13, 2018
    64
    @mengzhuo golang 不是全部都是协作式的,也有抢占式调度在里面跟 Python 这些语言不一样
    shiabo1121
    shiabo1121
    Apr 13, 2018
    65
    挂起
    to2false
    to2false
    Apr 13, 2018 via Android
    66
    让出
    honeycomb
    honeycomb
    Apr 13, 2018 via Android
    67
    叫避让,它的一个用处是开车的时候,转弯让对面直行的动作,就叫 yield
    CloudnuY
    CloudnuY
    Apr 13, 2018
    68
    丫的
    johnnie502
    johnnie502
    Apr 14, 2018
    69
    麻烦让一让
    Qlccks2
    Qlccks2
    Apr 14, 2018 via iPhone❤️3
    70
    另请高明
    shutongxinq
    shutongxinq
    Apr 14, 2018
    71
    @CSM 这个翻译可以和“常凯申”一拼高下。
    SuperMild
    SuperMild
    Apr 14, 2018
    72
    上面很多人说“避让”之类的,可能不对,比如

    a generator yields the values one at a time

    这是产出或返回一个数值,无法说避让那个数值啊。
    msg7086
    msg7086
    Apr 14, 2018
    73
    甩锅
    darluc
    darluc
    Apr 14, 2018
    74
    禅让咯?
    wavingclear
    wavingclear
    Apr 14, 2018
    75
    让出这个词有误解歧义,虽然出字是代表生产出这个含义。return 也可以返回值,没人说叫返出?
    wavingclear
    wavingclear
    Apr 14, 2018
    76
    让回,让停,让行。
    让回的返回值这么表达有点别扭。让停是自己视角,让行是生活用语,停车让行。让行应该最好。
    amai
    amai
    Apr 14, 2018
    77
    抛出
    qunwang6
    qunwang6
    Apr 14, 2018
    78
    让出+1
    tomato3
    tomato3
    Apr 14, 2018 via Android
    79
    产出
    zealinux
    zealinux
    Apr 14, 2018
    80
    让座
    chesterzzy
    chesterzzy
    Apr 14, 2018
    81
    走你
    andrewzhou
    andrewzhou
    Apr 14, 2018
    82
    蹦噔
    maxco292
    maxco292
    Apr 14, 2018 via Android
    83
    让权 ------ 让出执行权
    bellchu
    bellchu
    Apr 14, 2018
    84
    同交规
    watzds
    watzds
    Apr 14, 2018 via Android
    85
    谦让,退位
    Mutoo
    Mutoo
    Apr 14, 2018
    87
    yield: "诺"
    wdhwg001
    wdhwg001
    Apr 14, 2018 via iPhone
    88
    “让出”吧。

    我曾想过“让回”会不会比“让出”好,因为“让回”与“返回”更工整,且“让回”后面接名词表示内容而不是目标,如“让回一个苹果”和“让出这个座位”。

    但是…

    “返回”后面接名词是表示目标的,如“返回房间”。它本身也带来了理解的问题,如“返回函数 A ”是“以函数 A 返回”还是“返回到函数 A ”。

    在这个方面,“让出”反而与“返回”有着惊人的相似度,我们同样可以像理解“返回 0.15 ”一样理解“让出 0.15 ”的含义,这文法不通,但已是惯例。

    所以,“让出”比较好。
    wl99
    wl99
    Apr 14, 2018
    89
    一位搞理论研究的老师和我们说的是 产出
    yogogo
    yogogo
    Apr 14, 2018
    90
    生成器
    fansekey
    fansekey
    Apr 14, 2018 via Android
    91
    让出
    ech0x
    ech0x
    Apr 14, 2018 via iPhone
    92
    我又想起 enable 被翻译成“使能”的例子了。这种关键字还是不要轻易翻译了吧,多用用就会了。
    LuckCode
    LuckCode
    Apr 14, 2018 via iPhone
    93
    @nicevar 哈哈,笑出声
    liuxu
    liuxu
    Apr 14, 2018
    94
    上抛
    lishali12345
    lishali12345
    Apr 14, 2018
    95
    @Mutoo 棒呆
    mcfog
    mcfog
    Apr 14, 2018
    96
    所以从最开始标题就误导的不行,凭啥 yield 就成了协程的一部分了……那 return 是 OO 的一部分咯(滑稽
    miketeam
    miketeam
    Apr 14, 2018 via iPhone
    97
    野滴
    miketeam
    miketeam
    Apr 14, 2018 via iPhone
    98
    读书读傻了都,字面意思不就很好吗
    clino
    clino
    Apr 14, 2018 via Android
    99
    抛出(结果)
    jiangnanyanyu
    jiangnanyanyu
    Apr 14, 2018 via Android
    100
    我运行不下去了
    1  2  
    ❮ ❯
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1302 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 239ms · UTC 19:58 · PVG 03:58 · LAX 11:58 · JFK 14:58
    ♥ Do have faith in what you're doing.