[Python-ideas] PEP 505: None-aware operators

Chris Angelico rosuav at gmail.com
Tue Jul 24 09:08:10 EDT 2018


On Tue, Jul 24, 2018 at 11:02 PM, David Mertz <mertz at gnosis.cx> wrote:
> On Tue, Jul 24, 2018, 7:38 AM Rhodri James <rhodri at kynesim.co.uk> wrote:
>>
>> On 24/07/18 12:02, David Mertz wrote:
>> > Every use I've suggested for the magic proxy is similar to:
>> >
>> >    NullCoalesce(cfg).user.profile.food
>> >
>> > Yes, the class is magic. That much more so in the library I published
>> > last
>> > night that utilizes wrapt.ObjectProxy. But it's also pretty explicit in
>> > that an actual*word*  announces that funny stuff is going to happen on
>> > the
>> > same line.
>>
>>       Foo(cfg).user.profile.food
>>
>> Is that explicit that funny stuff is going to happen on the same line? I
>> wouldn't generally assume so, I'd just assume the coder created a throwaway
>> object to get at an attribute.
>
>
> Foo isn't a very indicative name. NoneCoalesce (or NullCoalesce, or
> GreedyAccess) is. But if I had any doubt, I could read the docstring for Foo
> to find out how magical it was, and in what way. I definitely know
> *something* is happening by creating that new instance in the line.
>

Okay. Check this out, then:

>>> x = Foo(cfg).user.profile
>>> x.food

Remember, these lines could be a very long way apart. You could pass
'x' to a function unrelated to the line of code that created it. And
'x.food' still has to have the magic. You can't mandate that the call
to Coalesce be on the same line as the attribute access - Python
doesn't work that way.

So your perfectly ordinary dot operator now does magic in addition to
normal attribute access. See why it's a dangerous thing?

ChrisA


More information about the Python-ideas mailing list