[Python-ideas] Null coalescing operators
Matthias Bussonnier
bussonniermatthias at gmail.com
Tue Sep 22 02:34:43 CEST 2015
On Mon, Sep 21, 2015 at 2:48 PM, Guido van Rossum <guido at python.org> wrote:
>
> In general to avoid this repetition you have to introduce a local variable,
> but that's often awkward and interrupts the programmer's "flow". The ?
> solves that nicely. The key issue with this proposal to me is how it affects
> readability of code that uses it, given that there isn't much uniformity
> across languages in what ? means -- it could be part of a method name
> indicating a Boolean return value (Ruby) or a conditional operator (C and
> most of its descendents) or some kind of shortcut.
>
> So this is the issue I have to deal with (and thought I had dealt with by
> prematurely rejecting the PEP, but I've had a change of heart and am now
> waiting for the PEP to be finished).
>
As we are in the process of writing a PEP and uses of ?/?? in other languages,
why not speak about thecurrent usage of `?` / `??` in the Python community ?
(I'll try to state only facts, excuse any passage that might seem like
a personal opinion)
Can the PEP include the fact that `? and `??` have been in use in the Scientific
Python community for 10 to 14 years now, and that any Scientific Python user who
have touched IPython will tell you that ? and ?? are for getting help.
(?? try to pull the source, while ? does not, but let's not get into details).
This include the fact that any IDE (like spyder) which use IPython under
the hood have this feature.
The usage of `?` is even visible on Python.org main page [3] (imgur screenshot),
which invite the user to launch an interactive console saying:
> object? -> Details about 'object', use 'object??' for extra details.
> In [1]:
leading the casual user thinking that this is a Python feature.
This fact is even including in Books and introduction to python. Sometime
without mentioning that the feature is IPython Specific, and does not work in
Python repl/scripts.
Examples in Cyrile's rossant "Learning IPython for Interactive
Computing and Data Visualization"[1]
introduce Python with the second code/repl example beeing about `?`.
Book extract :
> Some of these commands let you get some help or information about any
> Python function or object. For instance, have you ever had a doubt about how
> to use the super function to access parent methods in a derived class? Just type
> `super?` and you’ll find out. Appending `?` to any command or variable gives you all
> the information you need about it.
> In [1]: super?
> Type: type
> String Form:<type 'super'>
> Namespace: Python builtin
> ...
A google search also give for eaxample:
Python for beginners online tutorial[2] which does rapidly the same:
Tuto snippet:
> The "?" is very useful. If you type in `?` after a `len?`, you will see the
> documentation about the function len.
> Typing `?` after a name will give you information about the object attached to that name.
Doing even worse as they replace the IPython prompt `In[x]:` with
`>>>` literally showing
that `>>> len?` works. Which imply that it should work on a plain Python REPL.
As someone that have to regularly teach Python, and interact with new
Python users,
it will be hard to explain that `?` and `??` have different meaning
depending on the context,
and that most book on Scientific Python are wrong/inaccurate.
>From the current state of the PEP/proposal I'm guessing we should be
able to distinguish
Null Coalescing operation (or whatever name you want to give them) in
Python 3.6+
from actual help request, and that this will allow us to keep backward
compatibility
with 10+ years of code/user habits, but the result will most likely be
confusing.
It will be even harder if we have to remove the usage of `?`/`??`[4].
I also want to note that the use of `?`/`??` is not present to just being or end
of identifiers as it can also be used use to search for names:
> In [1]: *int*?
> FloatingPointError
> int
> print
But this usage is not as widespread as extracting help about objects,
and seem less relevant, though I'm not sure:
> In [10]: ?Float*Error
> FloatingPointError
>
> In [12]: Uni*Error?
> UnicodeDecodeError
> UnicodeEncodeError
> UnicodeError
> UnicodeTranslateError
Please take these fact into consideration when making a
decision/writing the Pep.
Thanks,
--
M
[1]: That's one of the only book for which I have (legally) the
sources, and that I bother to grepped through.
[2]: http://www.pythonforbeginners.com/basics/ipython-a-short-introduction
[3]: http://imgur.com/d0Vs7Xr
[4]: I'll have to hire a body guard to prevent people to pursue me to
the end of the earth with a chainsaw. I'm sure you know that feeling.
More information about the Python-ideas
mailing list