We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- Python
- Debugging
- Default Arguments
- Discussions
Default Arguments
Default Arguments
Sort by
recency
|
182 Discussions
|
Please Login in order to post a comment
This is a "gotcha" challenge. The test cases are not good examples of the gotcha that they're trying to get ya with.
Try this (pytest) test case and you'll see what the problem is:
The default value will be evaluated only once. Check the documentation below
Link - https://docs.python.org/3/tutorial/controlflow.html#default-argument-values
Simple code
`
python3
class EvenStream(object): def init(self): self.current = 0
class OddStream(object): def init(self): self.current = 1
def print_from_stream(n, stream=None): if stream is None: stream = EvenStream() for _ in range(n): print(stream.get_next())
queries = int(input()) for _ in range(queries): stream_name, n = input().split() n = int(n) if stream_name == "even": print_from_stream(n) else: print_from_stream(n, OddStream())
such a basic question yet so unclear instructions :(