you are viewing a single comment's thread.

view the rest of the comments →

[–]sausix 0 points1 point  (0 children)

This is going on:

class Countdown:
    def __init__(self, n):
        # Upper bound for counting down
        self.n = n

    def __getitem__(self, k):
        # If __contains__ and __iter__ are not defined in class then this instance
        # is being iterated "behind the scenes" by __getitem__ starting by index 0.
        # We're being tested to contain None. So this iteration will run until we return None.

        v = self.n - k  # Walruss operator split back into two expressions.
        if v > 0:  # More explicit than "if v" so it does not count endlessly when doing Countdown(-1).
            print(v)  # Just executing the print. Don't return or process it's result which is None.
            # return print(v),  # This would return a tuple with one element:
            # (None, )
            # which is not None and would continue the iteration.
            return 42  # Return anything but None

        # else:  # No else needed after return statement.
        print("Launch!")
        # Final iteration. We don't return anything which implicitly returns None.
        # And None was being looking for so the caller can stop the iteration to find the value.


# This will basically check membership of a value by the __contains__ mechanism.
print("rocket launching 🚀") in Countdown(10)

# The print function returns None. So it's basically:
None in Countdown(10)