Did OP basically summarise another blog post and publish it on their own blog, even though the original is linked? Not a good look, also because it's "narrated" in the first person, as if they were the one to implement this change.
I started by writing a Python grammar definition based on PLY (see http://www.dabeaz.com/ply/) , then tweaked it to handle the new feature and emit the right AST. It's when I discovered the following was valid Python:
>>> a=b=c=d=e=1
>>> del a, (b, (c, (((((d,e)))))))
I don't think PLY can handle the newest Python grammar, but I haven't looked into it.
You could argue that this is not merely adding syntax, but also adding the associated semantics.
Anyway, if you found this interesting, you might enjoy Eli Bendersky's blog post from nearly 15 years ago where he adds an "until ... do" statement to Python.
That blog post seems a lot more involved - it adds code to the bytecode compiler as well as to the parser.
I suspect that’s mostly because the `until` statement is more complex - but another factor seems to be Python’s change in parsing technology from LL(1) to PEG.
Yeah, this is the kind of material that shows that any attempt to prevent/hamper meta-programming or punt it to preprocessors is at best misguided. It also feeds the smug Lisp weenie within me.
Above all the wonders of Lisp's pantheon stand its metalinguistic tools; by their grace have
Lisp's acolytes been liberated from the rigid asceticism of lesser faiths. Thanks to Macro and
kin, the jolly, complacent Lisp hacker can gaze through a fragrant cloud of setfs and defstructs
at the emaciated unfortunates below, scraping out their meager code in inflexible notation, and
sneer superciliously. It's a good feeling.
-- iterate manual, A.1 Introduction
For all of the syntax features Python has been adding over the years, this would be a nice enhancement: making the "else None" optional in the ternary if-expression. E.g.,
spam = eggs if bar
# vs
spam = eggs if bar else None
So if "else None" is omitted, if bar is false, then does spam == None or is it unmodified? The former is what I think you want, but that would be very confusing.
Do we really need more syntactic sugar? Frankly, I am still confused why Python is going for a separate syntax for if expressions instead of just making its regular ifs into expressions
Be aware, however, that the inner circle will use you, take your contributions, and, if you develop own opinions, publicly ban and defame you without any possibility of setting any record straight.
pyhton-dev is a corporate shark tank where only personalities and employer matter (good code or ideas are optional).
You're Lisp-baiting, aren't you? ;) I'd add Elixir next to Nim (already mentioned); also Rust. Recently, also Scala.
The reason we don't have such metaprogramming available everywhere is mostly because you have to subscribe to a particular ideology to allow it. If you think programmers are generally intelligent and responsible, you put macros and metaclasses in your language. If, on the other hand, you think most programmers are dumb code monkeys (with a few exceptions, maybe) your language becomes like early Java or early Go.
You can almost do it with `condition and value`, but you get `False` instead of `None` (and adding any more code makes this worse than `value if condition else None`. Interestingly lua like `<condition> and <true-expression> or <false-expression>` ternaries actually work in python, with the added footgun that <true-expression> must be truthy and people will despise you).
Rust for example has a solution for this in std, there's a `bool::and_some` method (and `bool::then` that takes a closure to avoid eagerly evaluating the value), but `if { ... }` isn't an expression like `if { ... } else { ... }` is, probably to avoid coupling the language to `Option`.
nickpresta|1 year ago
sph|1 year ago
dalke|1 year ago
I started by writing a Python grammar definition based on PLY (see http://www.dabeaz.com/ply/) , then tweaked it to handle the new feature and emit the right AST. It's when I discovered the following was valid Python:
I don't think PLY can handle the newest Python grammar, but I haven't looked into it.For what it's worth, my Python grammar was based on an older project I did called GardenSnake, which still available as a PLY example, at https://github.com/dabeaz/ply/tree/master/example/GardenSnak... .
I've been told it's was one of the few examples of how to handle an indentation-based grammar using a yacc-esque parser generator.
nemoniac|1 year ago
Anyway, if you found this interesting, you might enjoy Eli Bendersky's blog post from nearly 15 years ago where he adds an "until ... do" statement to Python.
https://eli.thegreenplace.net/2010/06/30/python-internals-ad...
pansa2|1 year ago
I suspect that’s mostly because the `until` statement is more complex - but another factor seems to be Python’s change in parsing technology from LL(1) to PEG.
asplake|1 year ago
unknown|1 year ago
[deleted]
dig1|1 year ago
BoingBoomTschak|1 year ago
pkolchanov|1 year ago
I have worked on a similar series of CPython customization articles that you might find interesting:
https://github.com/pkolchanov/CustomizingPython
cpburns2009|1 year ago
genter|1 year ago
kstrauser|1 year ago
TwentyPosts|1 year ago
tgv|1 year ago
zitterbewegung|1 year ago
tmgehrmann|1 year ago
pyhton-dev is a corporate shark tank where only personalities and employer matter (good code or ideas are optional).
zahlman|1 year ago
The effort is very much appreciated.
m463|1 year ago
Y_Y|1 year ago
BiteCode_dev|1 year ago
I would say it's a good thing, I don't want to see a hundred of half baked, badly tested and vaguely document DSL with no decent tooling support.
klibertp|1 year ago
The reason we don't have such metaprogramming available everywhere is mostly because you have to subscribe to a particular ideology to allow it. If you think programmers are generally intelligent and responsible, you put macros and metaclasses in your language. If, on the other hand, you think most programmers are dumb code monkeys (with a few exceptions, maybe) your language becomes like early Java or early Go.
radarsat1|1 year ago
knighthack|1 year ago
bbb651|1 year ago
Rust for example has a solution for this in std, there's a `bool::and_some` method (and `bool::then` that takes a closure to avoid eagerly evaluating the value), but `if { ... }` isn't an expression like `if { ... } else { ... }` is, probably to avoid coupling the language to `Option`.
steveklabnik|1 year ago