3 ms·
You can also use coroutines for a pull-based model. In the end it just depends on whether you read or write data when you yield and both Lua and Python can do t
by ufo 14y ago
You can also use coroutines for a pull-based model. In the end it just depends on whether you read or write data when you yield and both Lua and Python can do that.
IIRC, the biggest difference between the languages is that Lua coroutines are "stackless" so you can have "non-generator" functions calling "generator" functions. In Python you need to use something like `yield from` and that turns the calling functions into generator functions as well.
- chubot 14y agoI don't think that's the case. "yield from" is apparently for generators, which allows yielding from "subgenerators" (i.e. a generator can invoke another generator which is part of its stream). But you can already the equivalent with Python coroutines (and Lua), using the trampolining technique in Beazley's material (and PEP 342). Python coroutines aren't any less powerful than Lua coroutines. If you still believe they are I would be very interested in an example.
- ufo 14y agoIts not about power its about expressiveness. For example, in Python you can't pass a generator function as an argument to `map`, since map doesn't use yields when calling its argument. On the other hand, in Lua the "generator-ness" is a runtime concept instead of a syntactic one so you can pass a generator to `map` and it will still work fine. I won't bother asking what model is the better one. The Python version is more rigid but its also more explicit...
- chubot 14y agoNo, Python has BOTH generators and coroutines. Lua has only coroutines. You're thinking of them as alternatives, whereas Python has a superset of what Lua has. Builtin map() is for generators only. You can write map for coroutines as well: def mapcoro(func, target): while True: value = (yield) target.send(func(value)) (untested, I think you need to catch StopIteration around the yield line) You can do everything with Python coroutines that you can do with Lua coroutines. You could say that Python doesn't need generators, but that's a different argument. I agree it is confusing... I was confused for a long time.