acaos / python-pep505

Python Polyfill for PEP 505 (None-aware / null-coalescing operators)
MIT License
27 stars 0 forks source link

Syntax proposal #5

Open mnieber opened 2 years ago

mnieber commented 2 years ago

Hi Doug,

I've been reading today about PEP 505 and I wondered if the following syntax could take away the complaint about new Python users finding the .? syntax unintuitive:

x = foo -> .help -> .get_user("doug") -> ["name"]

This would be the none-aware way to access foo.help.get_user("doug")["name"].

It comes down to using -> . instead of ?. and -> [ instead of ?[. I think that new users will intuitively understand that the -> arrows form a chain that might be broken at some point (and then None is returned).

Best, Maarten

ps I just came to the conclusion that I personally also find this syntax easier on the eyes than .?. It seems to work well on the code examples that you give in PEP505

cdce8p commented 2 years ago

I believe one of the arguments for ?. was that it's similar to optional chaining in the JavaScript world.

Additionally, -> is only used for return type annotations, currently. At least for Python there isn't an established pattern to use it for attribute access.

mnieber commented 2 years ago

I believe one of the arguments for ?. was that it's similar to optional chaining in the JavaScript world.

Yes, so that argument would fall away then. But people who say that ?. is not readable enough are not going to push for similarity to Javascript (because that would not make sense :-) ).

Additionally, -> is only used for return type annotations, currently. At least for Python there isn't an established pattern to use it for attribute access.

Correct, using it for attribute access would be a new pattern. In .get_user("doug") -> ["name"] the arrow can be interpreted as "on the thing that get_user returns, do ...", so the usage of the arrow is still related (imo) to how it's used for return types.