3 ms·
Python doesn't have a do..while loop, so when you want to do a thing at least once, the simplest replacement is starting the loop with "while 1:" and ending the
by markrages 5y ago
Python doesn't have a do..while loop, so when you want to do a thing at least once, the simplest replacement is starting the loop with "while 1:" and ending the loop with "if ... break".
I disapprove of any linter that flags this idiom.
- tempest_ 5y agowhy "while 1:" and not "while true:"?
- markrages 5y agoI think that's brain damage from years of embedded C.
- hermitdev 5y agoBecause "while 1:" is valid Python, but "while true:" is not ;) (gotta capitalize "True"). In addition to the GP's self deprecating sibling comment, I started using Python around 2.2, before there were even "True" and "False". So, seeing a 'while 1' loop is perfectly natural to me. But, I'm also perfectly comfortable with Python's "truthiness" in more places than most people are. As a bonus: consider "while 'false' :" as a perfectly valid start to an unbounded loop.
- chrismorgan 5y ago> consider "while 'false' :" as a perfectly valid start to an unbounded loop That sort of thing is ubiquitous in dynamically-typed languages. Even HTML: the state of boolean attributes is determined by the attribute being set, regardless of its value, so <input disabled=false> will give you a nice disabled text box. Mind you, some attributes that you might think would be boolean actually aren’t, for varying reasons good and bad, e.g. autocomplete=on|off, aria-hidden=true|false.
- fomine3 5y agoEnjoy YAML with Norway. https://news.ycombinator.com/item?id=17359376 https://news.ycombinator.com/item?id=17359376
- markrages 5y agoI learned Python around that time, when the docs said that `open()` is deprecated and to use `file()` as a constructor instead. Still unlearning that habit.
- mulmboy 5y agoCheers for the history lesson - True and False really are relatively new in Python. I wonder how common that knowledge is. http://python-history.blogspot.com/2013/11/the-history-of-bool-true-and-false.html http://python-history.blogspot.com/2013/11/the-history-of-bo... https://www.python.org/dev/peps/pep-0285/ https://www.python.org/dev/peps/pep-0285/
- atdt 5y agoIt used to be slightly faster in Python 2, because True could be reassigned, so its value had to be loaded and checked with every iteration.
- dragonwriter 5y ago> Python doesn't have a do..while loop Python doesn't have an “[repeat...]until” loop, which C misspells as “do...while”, which fails to express what is going on. > so when you want to do a thing at least once, the simplest replacement is starting the loop with "while 1:" and ending the loop with "if ... break". “while True:” is more idiomatic Python (“while 1:” works since 1 is truthy, but using a literal 1 for “True” is a C-ism, Python has True as a literal for quite some time and idiomatic Python uses it.)
- woopwoop 5y agoActually the most idiomatic way to write this is `while "False":`