From mboxrd@z Thu Jan 1 00:00:00 1970 From: random832@fastmail.com (Random832) Date: Sat, 11 Nov 2017 12:30:20 -0500 Subject: [TUHS] 80 columns ... In-Reply-To: <20171111170456.uq6tb63rtq6hkuc6@matica.foolinux.mooo.com> References: <1510334474.27585.for-standards-violators@oclsc.org> <20171111170456.uq6tb63rtq6hkuc6@matica.foolinux.mooo.com> Message-ID: <1510421420.1505476.1169232000.0EB6B20D@webmail.messagingengine.com> On Sat, Nov 11, 2017, at 12:04, Ian Zimmerman wrote: > On 2017-11-10 13:21, Norman Wilson wrote: > > -- I miss one particular case of assigment having a value: > > that of > > while ((val = function()) != STOP) > > do something with val > > I was once in a remote job interview with a Ruby shop. I don't know > Ruby, but they said I could use Python. Of course this situation came > up (it's pretty common when you think about it) and on this occasion a > whim made me write it thus: > > while True: > val = function() > if val == STOP: > break > do_something() > > Their reply was overflowing with shock and horror that I would use > "while True", and that was the end of that opportunity for me. > Apparently Ruby has a construct to handle this cleanly, without having > to call function() from two sites. Python has one as well, though it's a bit obscure - ISTR around half of the people who commented on a thread on the python mailing lists where this came up weren't familiar with it. for val in iter(function, STOP): do_something() If the function needs arguments, you have to use a lambda. If the condition is something other than equality to some value... well, you can hack something together, but it's debatably cleaner than the while True loop. (If the function is readline, though, you can just iterate over the file object)