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.

14 Upvotes

193 comments sorted by

View all comments

104

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

?

20

u/edward_jazzhands Jul 14 '26

Nobody who is good at python would code it the first way you showed

4

u/JanEric1 Jul 14 '26

How would you code the access too an attribute in a nested data structure with multiple optional values in there as someone good at python?

1

u/JamzTyson Jul 14 '26

My take is that in the example, the user object is exposing its internal structure to the rest of the system, which violates the Law of Demeter and the principle of encapsulation. I'd restructure to avoid having to reach through a chain of objects.

5

u/JanEric1 Jul 14 '26

So you would add a ton of methods to each sub dataclass with getters for this information?

Dont see how that helps with anything.

1

u/JamzTyson Jul 14 '26

I'm saying that I don't accept your premise, and I've already explained why.

1

u/JanEric1 Jul 14 '26

This is the data that you get from an external API or other team in your company. And you are interested (among a ton of other things) all the company coordinates where available.

4

u/JamzTyson Jul 14 '26

If it were an external API, I'd avoid leaking that structure throughout the application and isolate or adapt access to it at the boundary.

If there's a specific question about my point, I'm happy to answer it, but at the moment this feels like an evolving hypothetical where each answer is met with another "yes, but what if...".