`if` condition with `as` keyword

Hi to everybody. I was looking for any resolution for my pain in PEP and Stack Overflow and can't find an answer. As well the similar ideas here are 13 years old and completely missed. I feel that I need to create a garbage code doing things like: ``` if myDict.get('spam'): print(myDict.get('spam')) ``` or ``` x = myDict.get('spam') if x is not None: print(x) ``` or ``` days = seconds // 86400 if days > 1: print(days) ``` and I want to use the `as` keyword instead i.e. ``` if myDict.get('spam') as x: print(x) ``` ``` if myDict.get('spam') as x is None: print(x) ``` ``` if (seconds // 86400) as days > 1: print(days) ```

Use := operator (python 3.8+) https://www.python.org/dev/peps/pep-0572/ niedz., 26 kwi 2020 o 14:37 Николай Мостовенко <sofigenr@gmail.com> napisał(a):
Hi to everybody. I was looking for any resolution for my pain in PEP and Stack Overflow and can't find an answer. As well the similar ideas here are 13 years old and completely missed. I feel that I need to create a garbage code doing things like: ``` if myDict.get('spam'): print(myDict.get('spam')) ``` or ``` x = myDict.get('spam') if x is not None: print(x) ``` or ``` days = seconds // 86400 if days > 1: print(days) ```
and I want to use the `as` keyword instead i.e. ``` if myDict.get('spam') as x: print(x) ``` ``` if myDict.get('spam') as x is None: print(x) ``` ``` if (seconds // 86400) as days > 1: print(days) ``` _______________________________________________ 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/T3DV7X... Code of Conduct: http://python.org/psf/codeofconduct/
-- 闇に隠れた黒い力 弱い心を操る

Also, just to clearly demonstrate how the walrus operator could be used for the OP's examples: original: ``` if myDict.get('spam'): print(myDict.get('spam')) ``` walrus: ``` if (x := d.get("spam")): print(x) ``` original: ``` x = myDict.get('spam') if x is not None: print(x) ``` walrus: ``` if (x := d.get("spam")) is not None: print(x) ``` original: ``` days = seconds // 86400 if days > 1: print(days) ``` walrus: ``` if (days := seconds // 86400) > 1: print(days) ``` On Sun, Apr 26, 2020 at 9:04 AM Piotr Duda <duda.piotr@gmail.com> wrote:
Use := operator (python 3.8+) https://www.python.org/dev/peps/pep-0572/
niedz., 26 kwi 2020 o 14:37 Николай Мостовенко <sofigenr@gmail.com> napisał(a):
Hi to everybody. I was looking for any resolution for my pain in PEP and Stack Overflow and can't find an answer. As well the similar ideas here are 13 years old and completely missed. I feel that I need to create a garbage code doing things like: ``` if myDict.get('spam'): print(myDict.get('spam')) ``` or ``` x = myDict.get('spam') if x is not None: print(x) ``` or ``` days = seconds // 86400 if days > 1: print(days) ```
and I want to use the `as` keyword instead i.e. ``` if myDict.get('spam') as x: print(x) ``` ``` if myDict.get('spam') as x is None: print(x) ``` ``` if (seconds // 86400) as days > 1: print(days) ``` _______________________________________________ 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/T3DV7X... 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/ZJPMMG... Code of Conduct: http://python.org/psf/codeofconduct/
participants (3)
-
Kyle Stanley
-
Piotr Duda
-
Николай Мостовенко