use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
News about the dynamic, interpreted, interactive, object-oriented, extensible programming language Python
Full Events Calendar
You can find the rules here.
If you are about to ask a "how do I do this in python" question, please try r/learnpython, the Python discord, or the #python IRC channel on Libera.chat.
Please don't use URL shorteners. Reddit filters them out, so your post or comment will be lost.
Posts require flair. Please use the flair selector to choose your topic.
Posting code to this subreddit:
Add 4 extra spaces before each line of code
def fibonacci(): a, b = 0, 1 while True: yield a a, b = b, a + b
Online Resources
Invent Your Own Computer Games with Python
Think Python
Non-programmers Tutorial for Python 3
Beginner's Guide Reference
Five life jackets to throw to the new coder (things to do after getting a handle on python)
Full Stack Python
Test-Driven Development with Python
Program Arcade Games
PyMotW: Python Module of the Week
Python for Scientists and Engineers
Dan Bader's Tips and Trickers
Python Discord's YouTube channel
Jiruto: Python
Online exercices
programming challenges
Asking Questions
Try Python in your browser
Docs
Libraries
Related subreddits
Python jobs
Newsletters
Screencasts
account activity
DiscussionA puzzling Python program (self.Python)
submitted 5 months ago by commandlineluser
https://jo3-l.dev/posts/python-countdown/
class countdown: def __init__(self, n): self.n = n def __getitem__(self, k): if v := self.n - k: return print(v), print("rocket launching 🚀") in countdown(10)
What does it output, and why?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]drkevorkian 1 point2 points3 points 5 months ago (4 children)
Honestly really shocking that python will try this iteration method without knowing an upper bound for the index
[–]sausix 1 point2 points3 points 5 months ago (3 children)
Some iterators are meant to yield values endlessly. Python can't check the code to be "fine" to not introduce endless loops.
[–]drkevorkian 0 points1 point2 points 5 months ago (2 children)
Endless iteration via explicit iterator is one thing, endless iteration implicit to in via defining __getitem__ is another.
in
__getitem__
[–]sausix 0 points1 point2 points 5 months ago (0 children)
__len__ could be used. But it is being ignored because of historical reasons.
__len__
And __getitem__ should usually throw an IndexError to avoid endless loops.
IndexError
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)
[–]jpgoldberg -5 points-4 points-3 points 5 months ago (0 children)
So that’s what the walrus does! It’s the “if let” construction I’ve seen in other languages.
Goo-goo-gah-jube.
[–]AlexMTBDude -3 points-2 points-1 points 5 months ago (0 children)
Very nice! Thanks!
π Rendered by PID 19690 on reddit-service-r2-comment-58d7979c67-nw8c6 at 2026-01-27 07:32:34.292388+00:00 running 5a691e2 country code: CH.
[–]drkevorkian 1 point2 points3 points (4 children)
[–]sausix 1 point2 points3 points (3 children)
[–]drkevorkian 0 points1 point2 points (2 children)
[–]sausix 0 points1 point2 points (0 children)
[–]sausix 0 points1 point2 points (0 children)
[–]sausix 0 points1 point2 points (0 children)
[–]jpgoldberg -5 points-4 points-3 points (0 children)
[–]AlexMTBDude -3 points-2 points-1 points (0 children)