PEP 823: None-aware access operators

+1
IMHO one of the most important PEP’s Python has ever had.

Python is all about duck typing / dictionary-based code…yet always lacked operators to make safe access ergonomic.

Before this PEP, safe access in Python has always come with the disadvantage of seriously mangling your access syntax. Example:

:white_check_mark: GOOD:

  • foo.bar safe access foo?.bar
  • foo.bar safe access foo.bar ?? null
  • :white_check_mark: Autocomplete on properties still work!
  • :white_check_mark: Just add a few characters to existing code.
  • foo?['bar'] wont autocomplete, but at least it won’t mangle your code (add 1 character!).

:cross_mark: BAD:

  • foo.bar safe access if 'bar' in foo:
  • foo.bar safe access foo.get('bar')
  • :cross_mark: Will never autocomplete!
  • :cross_mark: Mangles your access syntax. Requires restructuring code. :sob:

JS, PHP, Ruby etc has had this forever. (PHP has had non-mangling safe access in version 1.0 with @ !!! But now has all the standard operators too!)

Super happy about the PEP- natural feature for dynamic languages.

2 Likes