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

Brice Parent contact at brice.xyz
Thu Jul 19 03:45:05 EDT 2018


Hi,
I think this breaks one of the most important aspect which makes Python 
more and more popular every year: its readability. Until now, you didn't 
need to know the language very well to have some understanding about 
what a script did. Some parts of it could be a bit complicated (like 
comprehensions, which we only allow in unit tests and small utilities 
where I work, exactly for this reason) or non-obvious (like the new := 
operator, but even if you don't get exactly what it does, it's not 
really cryptic either. It's still some-kind-of-assignment).
We've been teaching for years that there is virtually no cost in adding 
a few simple lines of code from times to times, and that it has to be 
done to improve readability, while this proposal's goal seems to be the 
opposite (make the code more compact at the cost of readability), which 
complicates the understanding of the whole language to save a few `if x 
is None:`. To me, it goes the opposite of most of the first lines of the 
zen of Python.

Where I work, we update most of our projects to use new Python versions 
almost immediately when a new one is out and the libs we use are 
compatible. This would probably change that; we would likely wait for a 
few years, the time for the feature to be used widely in other projects, 
and for us to have some real-life feedback to know what to do about it, 
like: are there some cases it really is justified? Does it make it 
harder/simpler to read/write python code for experts and non-experts? 
Should we add a rule to enforce its use, or to prevent it?

The biggest drawback of this, is that (if I understand it well), it may 
be done quite easily without any change to the language:

def first_set(*elements):  # please don't mind the name of the function, 
it's not the purpose here
     """ Will return the first element that is not None """
     for element in elements:
         if element is not None:
             return element

     raise AllNoneException()

first_set(3, 5)  # -> 3
first_set(None, 5)  # -> 5
first_set(None, None, 8, 10)  # -> 8
first_set(None, Car(model="sport")).buy()  # calling 
Car(model="sport").buy()
first_set(None, ["a", "b", "c"])[1]  # -> "b"
first_set(None, None)  # -> exception is raised

(note that such function could even accept a "rejected_values" kwarg, 
like `rejected_values=(None, [], "")`, just by replacing the `if` clause 
by `if element not in rejected_values:`)

I might have missed some implications, or even not understood the PEP at 
all, though!

- Brice






More information about the Python-ideas mailing list