Skip to content

Instantly share code, notes, and snippets.

@tranghaviet
Created April 5, 2017 16:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tranghaviet/d6e9862afe02553ef9063c5a2419fe8e to your computer and use it in GitHub Desktop.
Save tranghaviet/d6e9862afe02553ef9063c5a2419fe8e to your computer and use it in GitHub Desktop.
yeild in python
>>> def dummy():
... print "You won't see me when created"
... yield 1
... print "You didn't see me"
... yield 2
... print "Bye bye"
...
>>> gen = dummy()
>>> gen.next()
You won't see me when created
1
>>> gen.next()
You didn't see me
2
# lần tạo gen = dummy() thì hàm chưa được thực thi
# lần gọi gen.next() tiếp theo thì chương trình sẽ được thực thi
# và chạy tiếp ở các yield tương ứng
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment