Like the title says, I'm wondering why the default value in a next expression is initialized regardless of it being used. An example of this is below.
class Test(object):
def __init__(self):
print("Test object created")
print(next((i for i in range(1)), Test()))
Previously I would have thought this to only print out 0
and not Test object created
as well as 0
. I recently found out this wasn't the case through a bug caused by this behavior.
Is there some reason for this? Was this a design decision?
The design decision here is that next
is just an ordinary function, not special language syntax. And in any function call, all arguments are evaluated before the function starts executing. next
is nothing special, it just follows the same rules as everyone else.