[Python-Dev] assignment expressions: an alternative proposal
Anthony Flury
anthony.flury at btinternet.com
Tue Apr 24 17:29:11 EDT 2018
On 24/04/18 17:11, Yury Selivanov wrote:
> On Tue, Apr 24, 2018 at 12:03 PM, Ethan Furman <ethan at stoneleaf.us> wrote:
> [..]
>> But I do write this:
>>
>> def wrapper(func, some_value):
>> value_I_want = process(some_value)
>> def wrapped(*args, **kwds):
>> if value_I_want == 42:
>> ...
> But this pattern is more rare than comparing local variables. That's
> the point I'm trying to use. Besides, to make it an assignment
> expression under my proposal you would need to use parens. Which makes
> it even less likely that you confuse '=' and '=='.
Just because you wrap a set of character in parens doesn't mean that you
wont potentially mistype what you should type inside the parens. The
failure mode of in C :
if (a = 3)
do_something_with_a(a);
Is Incredibly common even with very experienced developers - so much so
that most linters flag it as a likely error, and I think gcc has an
option to flag it as a warning - even though it is valid and very
occasionally it is useful.
Also many developers who come to Python from languages such as C will
still place parens around conditionals - this means that a typo which
will cause a Syntax Error in current versions, but would cause a
potentially subtle bug under your implementation (unless you maintain
the rule that you can't rebind currently bound names - which renders the
whole idea useless in loops (as already discussed at length).
I also still can't think of a single other Python construct where the
semantics of an operator are explicitly modified by syntaxtic elements
outside the operator. For mathematical operators, the surrounding parens
modifies the grouping of the operators but not the semantics (* means *,
it is just the operands which potentially change).
You could argue that your proposal overloads the semantics of the parens
(similar to how braces are overloaded to implement dictionaries and set
literals), but I don't think that overloading the semantics of parens is
good idea.
If Python is going to do assignment expressions we shouldn't overload
parens in my opinion - we should have a separate operator - doing this
avoids needing to exclude rebinding, and makes such expressions
considerably more useful.
--
Anthony Flury
email : *Anthony.flury at btinternet.com*
Twitter : *@TonyFlury <https://twitter.com/TonyFlury/>*
More information about the Python-Dev
mailing list