47

In JavaScript, if I'm not sure whether every element of the chain exists/is not undefined, I can do foo?.bar, and if bar does not exist on foo, the interpreter will silently short circuit it and not throw an error.

Is there anything similar in Python? For now, I've been doing it like this:

if foo and foo.bar and foo.bar.baz:
    # do something

My intuition tells me that this isn't the best way to check whether every element of the chain exists. Is there a more elegant/Pythonic way to do this?

Xeoth
  • 893
  • 1
  • 10
  • 22
  • 4
    What you want is PEP 505 https://www.python.org/dev/peps/pep-0505 – brandonscript Apr 21 '21 at 15:17
  • That's exactly what I'm looking for but the maybe-dot and maybe-subscript operators haven't been added to Python (yet?) – Xeoth Apr 22 '21 at 16:09
  • Maybe? I'm not certain because I'm not privileged enough to use Py 3.8 yet, but it appears to have been ratified? – brandonscript Apr 23 '21 at 00:12
  • It's not in Python 3.8. PEP 505 is marked as Deferred which means no progress is being made. See discussion at https://discuss.python.org/t/pep-505-status/4612. – John Mellor Jun 28 '21 at 22:19

7 Answers7

14

Most pythonic way is:

try:
    # do something
    ...
except (NameError, AttributeError) as e:
    # do something else
    ...
theEpsilon
  • 1,561
  • 16
  • 25
soumya-kole
  • 641
  • 5
  • 16
13

You can use getattr:

getattr(getattr(foo, 'bar', None), 'baz', None)
theEpsilon
  • 1,561
  • 16
  • 25
12

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

{'foo': {'bar': 'baz'}}.get('foo', {}).get('bar')
Aliaksandr Sushkevich
  • 9,760
  • 6
  • 35
  • 41
5

You can use the Glom.

from glom import glom

target = {'a': {'b': {'c': 'd'}}}
glom(target, 'a.b.c', default=None)  # returns 'd'
5

Combining a few things I see here.

from functools import reduce


def optional_chain(obj, keys):
    try:
        reduce(getattr, keys.split('.'), root)
    except AttributeError:
        return None

optional_chain(foo, 'bar.baz')

Or instead extend getattr so you can also use it as a drop-in replacement for getattr

from functools import reduce


def rgetattr(obj, attr, *args):
    def _getattr(obj, attr):
        return getattr(obj, attr, *args)
    return reduce(_getattr, attr.split('.'), obj)

With rgetattr it can still raise an AttributeError if the path does not exist, and you can specify your own default instead of None.

Hielke Walinga
  • 2,378
  • 1
  • 14
  • 23
4

Combining some of the other answers into a function gives us something that's easily readable and something that can be used with objects and dictionaries.

def optional_chain(root, *keys):
    result = root
    for k in keys:
        if isinstance(result, dict):
            result = result.get(k, None)
        else:
            result = getattr(result, k, None)
        if result is None:
            break
    return result

Using this function you'd just add the keys/attributes after the first argument.

obj = {'a': {'b': {'c': {'d': 1}}}}
print(optional_chain(obj, 'a', 'b'), optional_chain(obj, 'a', 'z'))

Gives us:

{'c': {'d': 1}} None
Bernard Swart
  • 408
  • 5
  • 7
4

I like modern languages like Kotlin which allow this:

foo?.bar?.baz

Recently I had fun trying to implement something similar in python: https://gist.github.com/karbachinsky/cc5164b77b09170edce7e67e57f1636c

Unfortunately, the question mark is not a valid symbol in attribute names in python, thus I used a similar mark from Unicode :)

  • It should also be noted that this only works in python 3 because python 2 variables names do not allow unicode – smac89 Dec 08 '21 at 17:00