[Python-ideas] Null coalescing operators
Stephen J. Turnbull
stephen at xemacs.org
Tue Sep 22 04:34:45 CEST 2015
Matthias Bussonnier writes:
> 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.
But the syntax is extremely restrictive. Both "None? 1 + 2" and
"None ?" are SyntaxErrors, as are "a?.attr" and even "*int*? ".
Prefixing the help operator also gives help, and in that case
whitespace may separate the operator from the word (apparently defined
as non-whitespace, and any trailing detritus is ignored). Perhaps the
prefix form (a little less natural for people coming directly from
natural languages, I guess) should be emphasized -- there are no
proposals for unary prefix use of "?" or "??".
So, this is the kind of DWIM that I doubt will confuse many users, at
least not for very long. Do you envision a problem keeping IPython
facilities separate from Python language syntax? Technically, I think
the current rules that divide valid IPython requests for help from
Python syntax (valid or invalid) should continue to work. Whether it
would confuse users, I doubt, but there are possible surprises for
some (many?) users, I suppose.
Definitely, it should be mentioned in the PEP, but Python syntax is
something that Python defines; shells and language variants have to be
prepared to deal with Python syntax changes.
> leading the casual user thinking that this is a Python feature.
Casual users actually expect software to DWIM in my experience. The
rule "leading help or stuck-right-on-the-end help works, elsewhere it
means something else" (including no meaning == SyntaxError) is
intuitively understood by them already, I'm sure. Also, I rather
doubt that "casual users" will encounter "?." or "??" until they're
not so casual anymore.
> 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,
I've never had a question about the context-sensitivity of "%" in
IPython. Have you?
> and that most book on Scientific Python are wrong/inaccurate.
I'm afraid that's Scientific Python's cross to bear, not Python's.
> It will be even harder if we have to remove the usage of
Not to worry about that. IPython can define its own syntax for
parsing out help requests vs. Python syntax. I doubt you'll have to
modify the current rules in any way.
> 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 : *int*?
But again "*int* ?" is a SyntaxError. This is the kind of thing most
casual users can easily work with. (At least speakers of American
English. In email text, my Indian students love to separate trailing
punctuation from the preceding word for some reason, but they would
certainly learn quickly that you can't do that in IPython.)
Again, I agree it would be useful to mention this in the PEP, but as
far as I can see there really isn't a conflict. The main thing I'd
want to know to convince me there's a risk would be if a lot of users
are confused by "%quickref" (an IPython command) vs. "3 % 2" (a Python
More information about the Python-ideas