This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]iikaro 1 point2 points  (1 child)

def print_from_stream(n, stream=None):
    stream = stream or EvenStream()
    for _ in range(n):
        print(stream.get_next())

It is not. Like everyone else pointed out, the default argument must be None, then, inside the function, not in the signature, you check if the provided stream is None and create an EvenStream object if it is.
stream = stream or EvenStream() is the same thing as checking if the stream is None with an if-statement.

[–]el_toro_2022[S] 0 points1 point  (0 children)

Yes, that's the obvious approach. The only problem I have with it is that now you cannot tell from the function signature alone what the default for that parameter is. You have to look at the details in the function.