15

Is there a null propagation operator ("null-aware member access" operator) in Python so I could write something like

var = object?.children?.grandchildren?.property

as in C#, VB.NET and TypeScript, instead of

var = None if not myobject\
              or not myobject.children\
              or not myobject.children.grandchildren\
    else myobject.children.grandchildren.property
6
  • 2
    Related: Does Python have the Elvis operator?, Is there a Python equivalent of the C# null-coalescing operator? Commented Jan 5, 2020 at 12:50
  • @smci, OP is specifically asking about chained attribute access. There may well be a duplicate for that, but I don't think it's the one you've proposed. Commented Jan 5, 2020 at 12:55
  • @Chris: ok please retitle, retag and reword accordingly. (The original title was merely "null/None propagation in Python") Commented Jan 5, 2020 at 12:56
  • @Chris: the question title couldn't be understood without reading the body b) it has no tags by which it would be found by search. This can cause duplicates in future. Commented Jan 5, 2020 at 13:00
  • @smci, we're getting in the weeds here. The question has already been improved by your edits and somewhat less by mine. Let's not argue about what it originally was. I'll see if I can find additional tags for it. Commented Jan 5, 2020 at 13:02

1 Answer 1

32

No. There is a PEP proposing the addition of such operators but it has not (yet) been accepted.

In particular, one of the operators proposed in PEP 505 is

The "None-aware attribute access" operator ?. ("maybe dot") evaluates the complete expression if the left hand side evaluates to a value that is not None

Sign up to request clarification or add additional context in comments.

2 Comments

Is it still not implemented in 2023?
@SaeedNeamati, correct. PEP 505 is deferred, meaning "Inactive draft that may be taken up again at a later time".

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.