Re:Python has brought computer programming to a vast new aud...
by eesmith on Thursday July 19, 2018 @05:48PM
(#17570608)
In Python 2.x the loop variable is part of the containing scope:
In Python 3.x, it is not:
It is sometimes useful to know why something failed. The following Python 2 code will not work in Python 3:
I have mostly found this feature of Python 2 to be useful on the interactive shell, when trying to diagnose what caused an error in my list comprehension. Eg.
Oops, there are only 3 columns, not 4.
[ Parent
| on HN ]
Re:Python has brought computer programming to a vast new aud...
by w-m on Thursday July 19, 2018 @06:19PM
(#17570841)
When you use iPython you can just drop into the context of the error with %debug after it occurred, and then print the line there. No need for the bleeding scope on the language level.
Re:Python has brought computer programming to a vast new aud...
by kozlovsky on Friday July 20, 2018 @07:11AM
(#17574008)
In Python 3.8 you will be able to use assignment expressions:
[a := x*2 for x in (1, 2, 3)]
See PEP 572 -- Assignment Expressions: https://www.python.org/dev/peps/pep-0572/
Re:Python has brought computer programming to a vast new aud...
by aidos on Thursday July 19, 2018 @06:26PM
(#17570886)
I’m the same, I would never use that side effect in production code but I do miss having it in ipython though my use is slight different.
I often did:
>>> [d for d in drawings]
>>> [d.<tab> ...]
Because I’m lazy and I want the autocomplete to be there for me. It’s actually the only thing I miss from python 2 and I will admit that a couple of times the sideffects have caught me out in production code so I’m happy with the change.It’s worth noting that the latest python pep is going to give you a way to assign the variables in the outer scope again:
https://www.python.org/dev/peps/pep-0572/#simplifying-list-c...