Python Internals: Adding a new statement to Python
eli.thegreenplace.net
eli.thegreenplace.net
(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))))))But specifically, I'm never going to add new statements to the python interpreter.
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 :)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.
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.
The author even notes that 'until cond' is functionally identical to 'while not condition'. A fun exercise for the author, if a bit silly.
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.