Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Python Internals: Adding a new statement to Python (thegreenplace.net)
51 points by mnemonik on June 30, 2010 | hide | past | favorite | 12 comments


The Scheme version is not worthy of a blog post:

  (define-syntax until
    (syntax-rules ()
      ((until pred expr ...)
       (let loop ()
         (if (not pred)
	     (begin
	       (begin expr ...)
	       (loop)))))))
In dialects with WHEN syntax (mzcheme, Kawa, probably many others) it's even more concise:

  (define-syntax until
    (syntax-rules ()
      ((until pred expr ...)
       (let loop ()
	 (when (not pred)
	       (begin expr ...)
	       (loop))))))


Do you use any lisp code in ourdoings?


Yes, it's mostly Kawa Scheme, adapted for the web. You might notice http://brl.codesimply.net/ and http://ourdoings.com/ have the same IP address.


I will never, ever do this. But I still love this kind of article, that gives a deeper insight into how things work.


If you've never messed with real parsing and compiling before I highly recommend it. It's like lifting programmer weights, so many other problems seem easy afterwards.


Yes, I took compiler design in college, and even in the real world I've had lex and yacc coming out my ears.

But specifically, I'm never going to add new statements to the python interpreter.


If you like working on the compiler front-end, wait 'til you take a look at what happens in the back-end. Phenomenal.


General observation about until. I think it works better "linguistically" as a REPEAT/UNTIL (ala BASIC) and not as a negative while loop.

So block of code always gets run at least once. For eg. in Perl:

    my $x;
    do { 
        print "guess the number? ";
        $x = readline;
    } until $x == 10;
Disclaimer: Can't recall using until since I last played with BASIC. And that was many years ago :)


Great breakdown.

I tried something simpler once, trying to map str.format() to a binary operator in Py3k.

http://github.com/jcsalterego/py3k-atsign

Needless to say it didn't pass the python-ideas (or was that python-dev?) mailing list trial, mostly because it produced ambiguities that plagued the % operator, which prompted this new str method in the first place.


Can anybody write a macro statement this way?

Something like:

macro square(x): return exec("#{x}*#{x}")

might be useful.

Edit: can anybody help me how about the comments markup. I tried searching around but couldn't find anything.


"There should be one-- and preferably only one --obvious way to do it." 'while' is it, 'until' is not.

The author even notes that 'until cond' is functionally identical to 'while not condition'. A fun exercise for the author, if a bit silly.


From the section "A language-advocacy digression":

This article doesn’t attempt to suggest the addition of an until statement to Python. Although I think such a statement would make some code clearer, and this article displays how easy it is to add, I completely respect Python’s philosophy of minimalism. All I’m trying to do here, really, is gain some insight into the inner workings of Python.




Consider applying for YC's Fall 2026 batch! Applications are open till July 27.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: