5 ms·
This syntax would against the Python principle DRY (Don't Repeat Yourself): The "lazy" keyword conveys no information that doesn't exist in the function body.
by csense 14y ago
This syntax would against the Python principle DRY (Don't Repeat Yourself): The "lazy" keyword conveys no information that doesn't exist in the function body.
Also, Python has precedent for this scanning behavior. One of the fundamental features of Python is that the scope of a variable is determined by the presence of an assignment statement in a function. For example:
a = 1
def f():
print a # UnboundLocalError occurs here
a = 2
f()
If you know Javascript, think about how often you need to use the "var" keyword in that language, and how easy it is to accidentally pollute the global namespace by omitting it.
- gejjaxxita 14y agoThis 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.