4 ms·
Edit: The below is actually wrong > For years I’ve wanted to be able to write my own control flow structures, such as an each...else that runs an else block if
by adambard 13y ago
Edit: The below is actually wrong
> For years I’ve wanted to be able to write my own control flow structures, such as an each...else that runs an else block if the each is empty (Handlebars templates have this).
Actually, python does have a (little-known) `for...else` structure that does what you want.
http://psung.blogspot.ca/2007/12/for-else-in-python.html http://psung.blogspot.ca/2007/12/for-else-in-python.html
- dragonwriter 13y agopython's for/else doesn't do what the grandparent post asked for -- that post asked for the else clause to execute if the container that was being iterated over is empty, and in python's for/else (as your link notes) the else clause executes whenever the for clause terminates normally (that is, other than by a break statement). These are very different constructs.
- adambard 13y agoSo it does. I wonder what language I was thinking of then.
- Lazare 13y agoThis is a long shot, but could you be thinking of the Django template language? https://docs.djangoproject.com/en/dev/ref/templates/builtins/#for-empty https://docs.djangoproject.com/en/dev/ref/templates/builtins... <ul> {% for athlete in athlete_list %} <li>{{ athlete.name }}</li> {% empty %} <li>Sorry, no athletes in this list.</li> {% endfor %} </ul>