Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

You're right, it isn't. The presence of yield makes the return value of f() a generator object. The function body is not executed initially. When you call .next() on the generator object execution starts and runs until it hits the first yield, raising a NameError because g is not defined.

edit: typo



minor nit: it will not even reach the yield expression because it fails to resolve g:

  >>> import dis
  >>> f = lambda: g((yield))
  >>> dis.dis(f)
    1           0 LOAD_GLOBAL              0 (g)
                3 LOAD_CONST               0 (None)
                6 YIELD_VALUE
                7 CALL_FUNCTION            1
               10 RETURN_VALUE
  >>>




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: