[Python-ideas] PEP 505: None-aware operators
Steven D'Aprano
steve at pearwood.info
Sat Jul 21 21:54:19 EDT 2018
On Sun, Jul 22, 2018 at 01:56:35AM +0200, Giampaolo Rodola' wrote:
> On Thu, Jul 19, 2018 at 3:39 PM Steven D'Aprano <steve at pearwood.info> wrote:
> > Tens of thousands of non-English speakers have had to learn the meaning
> > of what might as well be meaningless, random sets of symbols (to them)
> > like "class", "import", "while" and "True". If they can do so, perhaps
> > we English-speakers should stop complaining about how hard it is to
> > memorise the meaning of a couple of symbols like ??.
>
> "class", "import", "while" and "True" are keywords, not symbols.
They are only key WORDS if you are an English speaker. If your language
doesn't use the Latin script, they don't even look like words. They look
like gibberish: ∌≇⊅∇∫
> A symbol is more cryptic than a keyword
I was using "symbol" in its most general sense, "a visible sign or
representation of an idea". Words, letters, icons, emblems etc are all
symbols. Sorry for being unclear.
> hence it comes at a higher cost in terms of readability.
It is not clear to me that this is always true even when it comes to
non-word symbols. I don't think that "+" is harder to read than
"standard_mathematics_operators_numeric_addition" for example.
It is possible to be too verbose as well as too terse. This is why we
aren't copying COBOL.
> which is the reason why conditionals use
> keywords instead symbols:
>
> - we say "and" instead of "&&"
[...]
Indeed we do. But we also say:
- we say "+" instead of "add"
- we say "//" instead of "floor division"
- we say "**" instead of "exponentiation"
- we say "&" instead of "bitwise AND"
- we say "f( ... )" instead of "call f with arguments ..."
etc. Python has no shortage of non-word symbols:
== != ~ - + * ** / // @ % ^ & | << >> < <= > >= [] ()
[...]
> > *Its just spelling*. If it is a useful and well-defined feature, we'll
> > get used to the spelling soon enough.
>
> Such an argument may apply to other languages but not Python.
I disagree. I think it applies equally to Python. Python functions and
keywords are usually English words, but not always:
def elif
have to be memorised even by English speakers. If we gained a function
or even a keyword from Italian, let's say "ripetere", would that really
change the nature of Python? I don't think so. English speakers are
adaptable, we don't so much borrow words from other languages as chase
them down the alley and mug them for new vocabulary.
The same applies to non-word symbols. Look at how quickly and easily
people adapted to @ (the commercial "at" sign, a variation of
multiplication) as a separator in emails, not to mention #hashtags.
I'll admit that the number and variety of new operators gives me some
reason to pause, but for the simplest and most obvious case, the
proposed ?? operator, I think that the fears about readability are
grossly exaggerated.
> The philosophy of the language is all about readability and beauty since
> day 1, and the main reason why Python got so successful despite being
> slow.
Let's talk about Python's readability: the first time I saw Python code,
I had *no idea* how to read it. This was Python 1.5, long before
comprehensions, @ decorator syntax, etc. I could read Pascal, Forth,
Hypertalk, BASIC and Fortran, and I looked at Python and couldn't make
heads or tails of it. It was full of cryptic idioms like:
for i in range(len(sequence))
which is meaningless until you learn what range does and learn to read
it as a for loop:
for i = 0 to len(sequence) - 1
And as for slice notation:
suffix = word[n:]
that might as well have been advanced quantum mechanics transliterated
into Korean by a native Navaho speaker for all I could read it. I knew
round brackets were used for function calls f() but the only experience
I had with square and curly brackets was in mathematics, where they are
used as second and third levels of brackets:
x = {[a (b+1)][b (a - 1)] - 1}/2
Now *that* I understood. What on earth was this mysterious {'a': 1}
syntax that I keep seeing in Python code?
My initial experience with this bizarre, cryptic language Python was so
off-putting that I put it aside for literally three or four years before
coming back to it.
Of course I believe that Python is, generally speaking, a beautiful and
elegant language, extremely readable. But readability doesn't exist in a
vacuum. We still need to learn the symbols of the language, not just
operators like ** % and // but words as well. To appreciate the elegance
of the language, we need to know the common idioms, as well as the
semantics of keywords and functions.
Unfortunately, ?? does have one disadvantage: even though it is used as
an operator in a number of languages, Google doesn't seem to have
indexed it. Goggling for "??" is unhelpful. But then googling for "@
python" is similarly unhelpful. Clearly decorators was a mistake. /s
> That's why we have mandatory indentation, to say one. We don't
> *have to* get used to idioms which can decrease readability. When we
> do there must be a good reason and the spelling should matter (I
> remember the debates about decorators' syntax).
Exactly. Years later, do we still think that @decorator syntax is
unreadable and unnecessary?
In my opinion, writing
expression if expression is None else default
is the *opposite* of Pythonic, it is verbose and the DRY violation is
inelegant (as well as inefficient). I'd much rather use:
expression ?? default
although with PEP 572 approved, there is an alternative:
temp := expression if temp is None else default
which avoids the DRY violation but is more verbose than even the first
version.
--
Steve
More information about the Python-ideas
mailing list