None Propagation in Python Chained Attribute Access [Duplicate]

None Propagation in Python Chained Attribute Access [Duplicate]

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

1 Answer

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

0
Marcus Vance
Author

Marcus Vance

Marcus Vance is a cybersecurity auditor and technology writer dedicated to educating the public about online safety, data privacy regulations, enterprise security, and emerging cyber threats.