Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 548
VBobCat Avatar asked Jan 05 '20 12:01

VBobCat


1 Answers

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

like image 100
Chris Avatar answered Nov 10 '22 04:11

Chris