search

Home  >  Q&A  >  body text

python - Call a function until a sentinel value 代码解读?

PHPzPHPz2893 days ago456

reply all(1)I'll reply

  • 迷茫

    迷茫2017-04-18 09:41:16

    Code on mobile phone.

    This code cleverly uses another form of iter:

    If two parameters are passed to iter: callable and sentinel, the returned generator will repeatedly call callable and yield its return value until the return value and sentinel are equal.

    Equivalent code:

    def iter(callable, sentinel):
        while True:
            val = callable()
            if val == sentinel: break
            yield val

    reply
    0
  • Cancelreply