Optional chaining
The difference between the optional chain operator and the primitive operator is only that when obj
is a null value (None
), the optional chain operator will return None
, and the primitive operator will throw an exception.
When trying to access object properties that may not exist, the optional chain operator will make the expression shorter and more concise.
Grammar
1 2 3 4 5 |
|
Examples of real code
1 2 3 4 5 6 7 8 |
|
Reference
- https://peps.python.org/pep-0505/