If it's a dictionary you can use get(keyname, value)

{'foo': {'bar': 'baz'}}.get('foo', {}).get('bar')
Answer from Aliaksandr Sushkevich on Stack Overflow
🌐
Wiingy
wiingy.com › home › learn › python › chaining comparison operators in python
Chaining Comparison Operators in Python
January 30, 2025 - We can chain three or more comparison operators to make more complex comparisons. For example: 1x = 5 y = 10 z = 15 if x < y < z: print("x is less than y, which is less than z") if x == y == z: print( A. Comparison Operator Syntax: The syntax for comparison operators in Python is as follows:
🌐
Delft Stack
delftstack.com › home › howto › python › python optional chaining
Optional Chaining in Python | Delft Stack
October 10, 2023 - Among the features in Python, optional chaining is a safe and concise way of accessing nested object properties.
Discussions

Add optional chaining of attributes - Ideas - Discussions on Python.org
Hey everyone! Other languages have the concept of optional chaining (javascript/swift maybe more). It enables developers to easily access nested attributes that might be null. Example of how it could be used. from typing import Optional class B: data: Optional[bool] class A: container: Optional[B] ... More on discuss.python.org
🌐 discuss.python.org
3
May 25, 2023
Optional chaining operator in Python
Careful. You're about to discover monads on your way to implementing bind or flatMap More on reddit.com
🌐 r/Python
24
16
August 5, 2025
Explain Chaining comparison operators in Python
Explore the fundamentals of Python comparison operators, including equality, inequality, and logical comparisons. More on accuweb.cloud
🌐 accuweb.cloud
1
June 19, 2024
What Is the Optional Chaining Operator, and How Does It Work?
TLDR: Optional Chaining Operator is for objects, not properties? https://www.w3schools.com/jS/js_2020.asp " The Optional Chaining Operator returns undefined if an object is undefined or null (instead of throwing an error)." /TLDR I’ve found a quiz question pretty confusing but I thought I ... More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
0
April 8, 2025
🌐
Blogger
deploytonenyures.blogspot.com › 2022 › 11 › python-optional-chaining-revisited.html
Deploy to nenyures: Python Optional Chaining Revisited
November 14, 2022 - I've been thinking again about that Python missing feature (Optional Chaining) I wrote about in this post. It seems like there are no plans to approve the PEP-505 (None aware operators), as there are quite a few opponents to the syntax.
🌐
Python documentation
docs.python.org › 3 › tutorial › classes.html
9. Classes — Python 3.14.3 documentation
There’s nothing special about instantiation of derived classes: DerivedClassName() creates a new instance of the class. Method references are resolved as follows: the corresponding class attribute is searched, descending down the chain of base classes if necessary, and the method reference is valid if this yields a function object.
🌐
Python.org
discuss.python.org › ideas
Add optional chaining of attributes - Ideas - Discussions on Python.org
May 25, 2023 - Hey everyone! Other languages have the concept of optional chaining (javascript/swift maybe more). It enables developers to easily access nested attributes that might be null. Example of how it could be used. from typing import Optional class B: data: Optional[bool] class A: container: Optional[B] a = A() if data := a?.container?.data: ... To do something similar today. try: if data := a.container.data: ... except (NameError, AttributeError) as e: pass Is this ...
Find elsewhere
🌐
Medium
medium.com › @guongle › refactoring-series-part-1-optional-chaining-14d69d459b57
Refactoring Series Part 1 — Optional Chaining | by Guong Le | Medium
April 20, 2023 - Optional chaining is not directly supported in Python as a language feature, but there are several ways to achieve similar behavior using existing language constructs.
🌐
Python.org
discuss.python.org › ideas
Add optional chaining of attributes - #6 by Declow - Ideas - Discussions on Python.org
May 26, 2023 - Hey everyone! Other languages have the concept of optional chaining (javascript/swift maybe more). It enables developers to easily access nested attributes that might be null. Example of how it could be used. from typi…
🌐
Python
peps.python.org › pep-0505
PEP 505 – None-aware operators | peps.python.org
September 18, 2015 - The function maybe() returns either a Something instance or a Nothing instance. Similar to the unary postfix operator described in the previous section, Nothing overrides dunder methods in order to allow chaining on a missing value.
🌐
Reddit
reddit.com › r/python › optional chaining operator in python
r/Python on Reddit: Optional chaining operator in Python
August 5, 2025 -

I'm trying to implement the optional chaining operator (?.) from JS in Python. The idea of this implementation is to create an Optional class that wraps a type T and allows getting attributes. When getting an attribute from the wrapped object, the type of result should be the type of the attribute or None. For example:

## 1. None
my_obj = Optional(None)
result = (
    my_obj # Optional[None]
    .attr1 # Optional[None]
    .attr2 # Optional[None]
    .attr3 # Optional[None] 
    .value # None
) # None

## 2. Nested Objects

@dataclass
class A:
    attr3: int

@dataclass
class B:
    attr2: A

@dataclass
class C:
    attr1: B

my_obj = Optional(C(B(A(1))))
result = (
    my_obj # # Optional[C]
    .attr1 # Optional[B | None]
    .attr2 # Optional[A | None]
    .attr3 # Optional[int | None]
    .value # int | None
) # 5

## 3. Nested with None values
@dataclass
class X:
    attr1: int

@dataclass
class Y:
    attr2: X | None

@dataclass
class Z:
    attr1: Y

my_obj = Optional(Z(Y(None)))
result = (
    my_obj # Optional[Z]
    .attr1 # Optional[Y | None]
    .attr2 # Optional[X | None]
    .attr3 # Optional[None]
    .value # None
) # None

My first implementation is:

from dataclasses import dataclass

@dataclass
class Optional[T]:
    value: T | None

    def __getattr__[V](self, name: str) -> "Optional[V | None]":
        return Optional(getattr(self.value, name, None))

But Pyright and Ty don't recognize the subtypes. What would be the best way to implement this?

🌐
GeeksforGeeks
geeksforgeeks.org › typescript › how-to-use-optional-chaining-with-arrays-and-functions-in-typescript
How to use Optional Chaining with Arrays and Functions in TypeScript ? - GeeksforGeeks
May 6, 2024 - In this approach, we are using optional chaining with array elements, where we are accessing the first author in the authors array of the geeksArticle object.
🌐
Quora
quora.com › What-are-the-pros-and-cons-of-doing-method-chaining-in-Python
What are the pros and cons of doing method chaining in Python? - Quora
Answer (1 of 2): The main con of method chaining in Python is that Python doesn’t behave in the same way as JavaScript or Ruby for this case. You can’t visualization data transformation as pipelines flowing through the methods. In JavaScript, you can do this: [code]var a = [4, 1, 3, 3, ...
🌐
Accuweb
accuweb.cloud › home › explain chaining comparison operators in python
Understanding Python Comparison Operators and Chaining
June 19, 2024 - In Python, these operators include equality (==), inequality (!=), and others such as less than (<), greater than (>), less than or equal to (<=), and greater than or equal to (>=). While these operators individually provide powerful tools for logical comparisons, chaining them together allows for even more concise and readable code.
🌐
Nikhil Akki's Blog
nikhilakki.in › understanding-method-chaining-in-python
Understanding Method Chaining in Python
July 22, 2023 - Configuration settings: Method ... options on an object, where each method call modifies a specific setting. Fluent APIs: Method chaining allows for the creation of fluent APIs, where the chain of method calls reads like a sentence, providing an intuitive and expressive coding style. Method chaining is a powerful technique in Python that enables ...
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
What Is the Optional Chaining Operator, and How Does It Work?
April 8, 2025 - TLDR: Optional Chaining Operator is for objects, not properties? https://www.w3schools.com/jS/js_2020.asp " The Optional Chaining Operator returns undefined if an object is undefined or null (instead of throwing an error)."
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › Optional_chaining
Optional chaining (?.) - JavaScript | MDN
The optional chaining (?.) operator accesses an object's property or calls a function. If the object accessed or function called using this operator is undefined or null, the expression short circuits and evaluates to undefined instead of throwing an error.
🌐
scikit-learn
scikit-learn.org › stable › modules › generated › sklearn.decomposition.PCA.html
PCA — scikit-learn 1.8.0 documentation
Otherwise, if the input data is larger than 500x500 and the number of components to extract is lower than 80% of the smallest dimension of the data, then the more efficient “randomized” method is selected. Otherwise the exact “full” SVD is computed and optionally truncated afterwards.
🌐
Python.org
discuss.python.org › ideas
Introducing a Safe Navigation Operator in Python - #40 by steve.dower - Ideas - Discussions on Python.org
October 9, 2023 - I’ve been considering the idea of proposing a new feature in Python - a Safe Navigation Operator, similar to what’s available in languages like JavaScript, Ruby, and C#. Before proceeding with writing a formal PEP, I wan…
🌐
Python.org
discuss.python.org › ideas
Introducing a Safe Navigation Operator in Python - Page 4 - Ideas - Discussions on Python.org
October 14, 2023 - This post summarizes my thoughts on this operator. As a Node developer, I really see the usefulness of this operator in my day-to-day work because of JavaScript’s particularities, but in the context of Python I have mixed feelings. Python’s data model is stricter and the built-in attribute ...