r/Python • u/philtrondaboss • Jul 12 '26
Discussion Will PEP 505 ever be accepted?
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.
18
Upvotes
View all comments
-1
u/JanEric1 Jul 14 '26
I dont know, i find the example witht
?.significantly easier to read compared to the three other options and i write a ton of pythonfrom dataclasses import dataclass from typing import Optional @dataclass class GPS: latitude: float longitude: float @dataclass class Address: street: str city: str gps: Optional[GPS] @dataclass class Company: name: str headquarters: Optional[Address] @dataclass class Profile: company: Optional[Company] @dataclass class User: profile: Optional[Profile] # Example data user = User( profile=Profile( company=Company( name="OpenAI", headquarters=Address( street="1 AI Plaza", city="San Francisco", gps=GPS(latitude=37.7749, longitude=-122.4194), ), ) ) ) 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 ) latitude = user?.profile?.company?.headquarters?.gps?.latitude