question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Introduce new operator for syntactic sugar for is None and is not None

See original GitHub issue

I 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:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
ymeinecommented, Nov 23, 2017

@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:

  • consistency with the existing None aware operators, to have a comprehensive set
  • a bit more conciseness: obj is None becomes obj??

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 becomes a ?? b which is far more valuable, especially when starting to add more operands in the expression)

1reaction
evhubcommented, Nov 19, 2017

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found