Introduce new operator for syntactic sugar for is None and is not None
See original GitHub issueI quite often use None as a fallback/uninitialized value, e.g. for my:str? = None
.
To test it, I have to write if my is None
or if my is not None
.
I can use the ??
operator in some situations, but if an expression depends on the value being None or not, it doesn’t: result = a() if my is None else b(my)
What about introducing a is None operator?
result = a() if ?= my else b(my)
or if ?~ my: result = b(my); else result = a()
That would be an unary operator similar to not, but checking for the object being None or not.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:1
- Comments:10 (6 by maintainers)
Top Results From Across the Web
Syntactic Sugar for 'is not None' - Discussions on Python.org
My idea would be to introduce syntactic sugar for the not None construct, e.g. a Some or Any keyword (or anything else that...
Read more >How useful is C#'s ?? operator? - syntactic sugar
The ?? operator is like the coalesce method in SQL, it gets you the first non-null value. var result = value1 ?? value2...
Read more >Adding None-aware operators to Python? - LWN.net
PEP 505 ("None-aware operators") would provide some syntactic sugar, in the form of new operators, to handle cases where variables might be the...
Read more >operator overloading - Standard C++
Operator overloading allows C/C++ operators to have user-defined meanings on user-defined types (classes). Overloaded operators are syntactic sugar for ...
Read more >Unravelling `not` in Python - Tall, Snarky Canadian
For this next blog post in my series of Python's syntactic sugar, I'm tackling what would seem to be a very simple bit...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@evhub I am not the requester of the feature. But that’s true I do think it makes sense to have the unary equivalent of the binary operator
??
. The mention of the “or-like feature” in LiveScript was just to explain how the operator works there, highlighting that it’s using the same special characters for both unary and binary versions. I’m aware the binary version already works in Coconut, and that’s actually the unary equivalent which is being requested.Therefore yes, the two benefits are only:
obj is None
becomesobj??
Of course this is already doable is a non-ambiguous and non-cumbersome way, compared to what the binary version solved (
a if a is not None else b
becomesa ?? b
which is far more valuable, especially when starting to add more operands in the expression)What’s wrong with
obj is None
? It’s short, descriptive, and gets the job done. This feels to me like the one None-aware operator use case where Python really already has this down.