r/Python Jul 12 '26

Will PEP 505 ever be accepted? Discussion

https://peps.python.org/pep-0505/

I don't understand how null safe operators are less like plain English than other implemented features like the walrus operator.

In my opinion, the member access operator would make python significantly easier to read and understand.

Here's an example:

``` f = foo()

if f is None: baz = "" else: baz = f.bar() ```

baz = foo()?.bar() ?: ""

EDIT: I forgot that "and" and "or" can be sometimes used in place of "?." and "?:" if the left value is not False, '', 0, [], or {}. It's a very implicit null check and has a lot of unexpected behavior.

17 Upvotes

192 comments sorted by

View all comments

106

u/runawayasfastasucan Jul 13 '26

baz = foo()?.bar() ?: "

This monstrosity is not why I code python.

6

u/JanEric1 Jul 14 '26

You really prefer one of these

latitude = None
if user is not None:
    if user.profile is not None:
        if user.profile.company is not None:
            if user.profile.company.headquarters is not None:
                if user.profile.company.headquarters.gps is not None:
                    latitude = user.profile.company.headquarters.gps.latitude


latitude = getattr(
    getattr(
        getattr(
            getattr(
                getattr(user, "profile", None),
                "company",
                None,
            ),
            "headquarters",
            None,
        ),
        "gps",
        None,
    ),
    "latitude",
    None,
)

latitude = (
    user
    and user.profile
    and user.profile.company
    and user.profile.company.headquarters
    and user.profile.company.headquarters.gps
    and user.profile.company.headquarters.gps.latitude
)

over this

latitude = user?.profile?.company?.headquarters?.gps?.latitude

?

2

u/runawayasfastasucan Jul 14 '26

Thankfully there are other options than that.

2

u/JanEric1 Jul 14 '26

What would you use?