Default Arguments

Sort by

recency

|

202 Discussions

|

  • + 0 comments

    class EvenStream: def init(self): self.current = 0

    def get_next(self):
        next_value = self.current
        self.current += 2
        return next_value
    

    class OddStream: def init(self): self.current = 1

    def get_next(self):
        next_value = self.current
        self.current += 2
        return next_value
    

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

    if name == "main": queries = int(input()) for _ in range(queries): stream_name, n = input().split() n = int(n) if stream_name == "even": print_from_stream(n) elif stream_name == "odd": print_from_stream(n, OddStream())

  • + 0 comments
    def print_from_stream(n, stream=None):
        if stream is None:  # Ensure a new instance is created for default behavior
            stream = EvenStream()
        for _ in range(n):
            print(stream.get_next())
    
  • + 0 comments

    with no code given, im wondering where to debug

  • + 0 comments

    class EvenStream(object): def init(self): self.current = 0

    def get_next(self):
        to_return = self.current
        self.current += 2
        return to_return
    

    class OddStream(object): def init(self): self.current = 1

    def get_next(self):
        to_return = self.current
        self.current += 2
        return to_return
    

    def print_from_stream(n, stream=EvenStream()): stream.init() 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())

  • + 0 comments

    This is the proper way to handle default values like in this scenario - https://pylint.pycqa.org/en/latest/user_guide/messages/warning/dangerous-default-value.html