osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Python-Dev] Call for prudence about PEP-572


On Mon, Jul 9, 2018 at 3:14 AM, Giampaolo Rodola' <g.rodola at gmail.com> wrote:
>
>
> On Sun, Jul 8, 2018 at 6:45 PM Steve Holden <steve at holdenweb.com> wrote:
>>
>> On Sun, Jul 8, 2018 at 10:41 AM, Giampaolo Rodola' <g.rodola at gmail.com>
>> wrote:
>>>
>>> [...]
>>> I find that (space between the parentheses of a function call statement)
>>> too unnatural as a place where to put an assignment. It is not even
>>> "guarded" by a keyword like "if" or  "while" which can help as indicators
>>> that an assignment may occur. Also, I think it's way too easy to confuse it
>>> with a keyword argument:
>>>
>>>     >>> foo(x = 1)  # keyword arg
>>>     >>> foo(x := 1)  # assignment + value passing
>>> [...]
>>
>>
>> But the PEP 8 spellings are
>>
>>     foo(x=1)
>>
>> and
>>
>>    f(x := 1).
>>
>> The extra spacing makes it obvious that this isn't a regular named
>> argument.
>
>
> What if the author of the code I'm reading didn't respect PEP-8? I don't
> think it's fair to invoke PEP-8 as a counter-measure to obviate a syntax
> which can clearly be mistaken with something else simply by omitting 2
> spaces. Not to mention that I don't see why anyone would want to declare a
> variable in there in the first place.
>

It's not about why someone would want to assign inside a function
call. It's about why it should be forbidden. Perhaps nobody has a good
reason to use THlS_OBJECT as a variable name, and it's potentially
very confusing; but should the grammar of Python forbid it? No.
Because there is no value in forbidding it.

Python's grammar has a number of weird edge cases due to necessity
(for instance, "for x in a if cond else b:" works, but not in a
comprehension), and when there's an actual conflict, sure, you can say
"but nobody would ever want to do that, so we'll forbid it". In this
case, there is no conflict.

ChrisA