top | item 41868168

Adding syntax to the CPython interpreter

161 points| leontrolski | 1 year ago |leontrolski.github.io

57 comments

order

nickpresta|1 year ago

The linked, more detailed post in the article is far more interesting: https://miguendes.me/what-if-python-had-this-ruby-feature

sph|1 year ago

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.

dalke|1 year ago

In the bygone era of 2008, I had fun adding Perl/Ruby-style pattern matching.

  for line in open("python_yacc.py"):
      if line =~ m/def (\w+)/:
          print repr($1)
See http://www.dalkescientific.com/writings/diary/archive/2008/0...

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.

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

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.

https://eli.thegreenplace.net/2010/06/30/python-internals-ad...

pansa2|1 year ago

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.

asplake|1 year ago

I sometimes think it would be fun to do “do: ... where:/while/until”, wherein the “where” part does local bindings.

dig1|1 year ago

This is where Lisp-like languages excel with macros. An example in Clojure:

    (defmacro do* [body expr cond]
     `(~expr ~cond
       ~body))

     (do* (println "hello") if true)
     (do* (println "hello") when-not (> 1 2))

BoingBoomTschak|1 year ago

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
do..while is also the example used here https://www.tcl-lang.org/man/tcl9.0/TclCmd/uplevel.html

cpburns2009|1 year ago

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

genter|1 year ago

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.

kstrauser|1 year ago

I almost never use None as the second value here. From my POV, that would be new syntax for an unlikely situation.

TwentyPosts|1 year ago

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

tgv|1 year ago

Great, now you have the dangling else in python, the one thing that actually was solved by significant white space.

zitterbewegung|1 year ago

This is a cool article. If you want to understand how to contribute to python there is a guide at https://devguide.python.org

tmgehrmann|1 year ago

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).

zahlman|1 year ago

>Condensed version of this cool blog post.

The effort is very much appreciated.

m463|1 year ago

I never knew this syntax worked in python 2 and 3

  1 if True else 2

Y_Y|1 year ago

If only there was a language that let you modify the interpreter on the fly so you could do this as part of normal execution...

BiteCode_dev|1 year ago

Python can actually do this using "# coding:", albeit less elegantly than lisp.

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

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.

radarsat1|1 year ago

Why would I want to extend the language syntax as part of normal execution?

bbb651|1 year ago

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`.

steveklabnik|1 year ago

Option is already a lang item, so that would t be an issue. I don’t know what the actual underlying reason is, or if it was ever even proposed.