Feature or enhancement
Proposal:
Motivation
Python's existing x if cond else y ternary requires more characters and
some developers coming from C-family languages find cond ? x : y more
concise for simple one-line conditionals. Additionally, a $name shorthand
for declaring a variable pre-initialized to None (similar to Dart's
declaration semantics) could reduce boilerplate for patterns like:
x = None
...
x = some_value
Proposed syntax
$name # equivalent to: name = None
cond ? a : b # equivalent to: a if cond else b
Current status
I have a working prototype implemented at the grammar level
(Grammar/python.gram) as a proof of concept, tested against CPython's
own PEG parser generator, covering: precedence, nested ternaries,
list/dict/set comprehensions, generator expressions, lambda, walrus,
keyword arguments — with one known issue (f-string compatibility)
still being investigated.
Branch: https://github.com/RAJ0793/cpython/tree/features-clean
Questions for the community
- Is there existing prior art / rejected PEPs covering this exact syntax?
- Would this need a formal PEP given it's new operator syntax?
- Is there appetite for this kind of shorthand in the language, or does
it conflict with "there should be one obvious way to do it"?
I'm a newer contributor and want to follow the right process — happy to
close this if it's already been discussed and rejected before.
Has this already been discussed elsewhere?
No response given
Links to previous discussion of this feature:
No response
Linked PRs
Feature or enhancement
Proposal:
Motivation
Python's existing
x if cond else yternary requires more characters andsome developers coming from C-family languages find
cond ? x : ymoreconcise for simple one-line conditionals. Additionally, a
$nameshorthandfor declaring a variable pre-initialized to None (similar to Dart's
declaration semantics) could reduce boilerplate for patterns like:
Proposed syntax
Current status
I have a working prototype implemented at the grammar level
(Grammar/python.gram) as a proof of concept, tested against CPython's
own PEG parser generator, covering: precedence, nested ternaries,
list/dict/set comprehensions, generator expressions, lambda, walrus,
keyword arguments — with one known issue (f-string compatibility)
still being investigated.
Branch: https://github.com/RAJ0793/cpython/tree/features-clean
Questions for the community
it conflict with "there should be one obvious way to do it"?
I'm a newer contributor and want to follow the right process — happy to
close this if it's already been discussed and rejected before.
Has this already been discussed elsewhere?
No response given
Links to previous discussion of this feature:
No response
Linked PRs