
On 04/08/2023 14:02, Niktar Lirik via Python-ideas wrote:
Hi Daniil.
Yes, you can do almost same:
tt = 5 while tt := tt - 1: print(tt)
"almost" is right. The OP's version, as far as I can tell, wants to do post-decrement (test tt, then decrement it) so it would "do something" with tt equal to 0. The above Python code does pre-decrement (decrement tt then test it) and will exit the loop after printing 1. That could be got round e.g. by changing the middle line to the more clumsy while (tt := tt - 1) + 1: or while [tt, tt := tt - 1][0]: A problem (not the only one, I am sure!) with adding post-increment and post-decrement operators to Python is that the pre-increment and pre-decrement operators cannot be added in the same way. Because these: ++t --t are already legal Python syntax, applying the unary plus or unary minus operator twice, and changing that would break existing code. Python is not going to be turned into C. I suspect that by "Pythonic" you really mean "I'm familiar with it, it feels natural and intuitive TO ME". Other people's mileage will vary. Best wishes Rob Cliffe
04.08.2023 9:18, Daniil.arashkevich@gmail.com пишет:
Currently in Python we have construction like this:
tt = 5 while t: # do something tt -= 1
It would be great if in Python we have something like this: tt = 5 while (tt--): # do something
It is exists in C++. And in my opinion it is very Pythonic _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-leave@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/GRIWCD... Code of Conduct: http://python.org/psf/codeofconduct/
Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-leave@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/RRGE4B... Code of Conduct: http://python.org/psf/codeofconduct/