What’s the best Python idiom for this C construct?
while ((x = next()) != END) { .... }
I don’t have the ability to recode next().
update: and the answer from seems to be:
for x in iter(next, END): ....
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Short answer: there’s no way to do inline variable assignment in a while loop in Python. Meaning that I cannot say:
Since that’s not possible, there are a number of ‘idiomatically correct’ ways of doing this:
Obviously, this is kind of ugly. You can also use one of the ‘iterator’ approaches listed above, but, again, that may not be ideal. Finally, you can use the ‘pita pocket’ approach that I actually just found while googling:
Now you can do:
Thanks for this question; learning about the
__call__idiom was really cool! 🙂EDIT: I’d like to give credit where credit is due. The ‘pita pocket’ idiom was found here