4 ms·
This is an insightful point. I tend to regard this scanning behaviour as a "quirk" rather than something fitting in more with some deeper Python philosophy. A
by gejjaxxita 14y ago
This is an insightful point. I tend to regard this scanning behaviour as a "quirk" rather than something fitting in more with some deeper Python philosophy.
A specific syntax for generators would in my view be generally superior, containing the same number of new keywords (generator now replacing yield) and providing enhanced clarity.
generator hello(n):
for i in range(n):
return i
I am generally neutral about variable scope being determined by the assignment of a statement in a function - in 99.99% of cases the intuitive thing happens.
- eurleif 14y agoExcept you can use `return` without argument to return early from a generator, and `yield` without argument yields None, so they need to be separate keywords.
- gejjaxxita 14y agoThis functionality is so arcane that it's not needed.
- eurleif 14y agoEarly return is definitely needed. I guess you could make bare `return` early return, and `return None` the way to yield None, but that's kind of confusing.
- gejjaxxita 14y agoI agree that would be confusing, but I question that early return is needed - I don't think I've ever seen a generator example where early return is used. It could be that it's because I haven't seen enough examples of course. Please share some examples of a generator with early return which can't be accomplished easily and clearly in some other way.