[Python-Dev] PEP 572: Assignment Expressions
Steven D'Aprano
steve at pearwood.info
Sat Apr 21 03:11:18 EDT 2018
On Sat, Apr 21, 2018 at 12:30:36PM +1000, Chris Angelico wrote:
> There's that word "readability" again. Sometimes I wish the Zen of
> Python didn't use it, because everyone seems to think that "readable"
> means "code I like".
In fairness, if one can't read code, then one can hardly be expected to
like it.
But there's plenty of code I can read that I don't like.
However your point still stands: since we don't have an objective and
empirical measure of readability, we cannot help but be subjective about
it. And with such subjective judgements, it is very, very hard to
divorce personal opinions about what we like from subjective estimates
of how readable something is.
[...]
> Perfectly self-contained. They do everything in their own scope.
> Except ... except that they don't.
[...]
They being comprehensions inside class scopes.
Class scopes are already a bit weird, and don't *quite* work the
same as non-class scopes even without introducing comprehensions:
py> class X:
... a = 99
... b = lambda: a+1
... c = b()
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 4, in X
File "<stdin>", line 3, in <lambda>
NameError: name 'a' is not defined
I get bitten by this all the time. No, I tell a lie: I *hardly ever* get
bitten by this. Judging by the number of questions about it on
StackOverflow and the Python-List and Tutor mailing lists, I'd say that
I'm not unusual here.
> Introducing expression assignments will make these oddities even more
> obvious. You'd be able to demonstrate things like this at function
> scope, not just with a class.
In what way?
And are you absolutely sure they will be oddities? To give an analogy, I
don't think this is an oddity:
py> def func(a=1, b=a+1):
... pass
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined
If anyone expected that the default value for b could make use of the
default value for a, the answer is: no, Python function declarations
don't work that way. Maybe they could, if we wanted them to, but we
don't, so they don't.
So can you explain specifically what odd function-scope behaviour you
are referring to? Give an example please?
--
Steve
More information about the Python-Dev
mailing list