Getting in to metaprogramming
Aaron Brady
castironpi at gmail.com
Wed Nov 26 20:10:11 EST 2008
On Nov 26, 6:29 pm, Steven D'Aprano
<ste... at REMOVE.THIS.cybersource.com.au> wrote:
> On Thu, 27 Nov 2008 06:41:47 +0200, Hendrik van Rooyen wrote:
> > I am using the term in the restricted sense of Python writing Python
> > source.
>
> > Given that, can anybody think of an example that you could not do with a
> > class? (excepting the "stored procedure" aspect)
>
> GUI designer. You write a program to let the user create code by clicking
> buttons, dragging objects, drawing lines, etc. The GUI designer may use
> classes, but the purpose of those classes is to generate source code.
I want to disagree with this example. I hold that the proper output
of a GUI designer is in XML, or another data format.
> Testing code speed... you might have some functions with a loop, and you
> want to unroll the loop as an optimization. If you have one function, you
> can unroll it yourself. If you have a hundred such functions, you might
> want to write a program to do it. (Yes, I'm stretching...)
>
> Don't like that Python doesn't optimize tail-recursion? Then write a
> source-code analyzer that detects tail-recursion and re-writes the
> function using a while loop.
I've seen a preprocessor come up a few times. The input is a
program's source, and the output is source. Of course you can do it
with quotes and 'exec' at run-time, but if your whole file is quoted,
it may be a better option.
(Part of my rationale was I wanted syntax-coloring.)
> >> Thinking further back, when I was young and programming in Apple's
> >> Hypercard 4GL, I used to frequently use Hypercard scripts to generate
> >> new Hypercard scripts. That was to work around the limitations of the
> >> scripting language.
>
> > What sort of stuff did you do, and would having had simple OO available
> > have rendered it unnecessary?
>
> It's been 20-odd years, and the examples were pretty trivial... I don't
> really recall exactly, but it would have been something like this:
>
> * design a GUI involving lots of buttons on screen, each one with quite
> similar but not identical code;
>
> * since Hypercard didn't have a layout manager, write a script to
> generate each button, place it where needed, and set the button's code.
snip
(Above.) I'm not sure if you'd count something like 'perlmodule':
import perl
# Simple arithmetics
six = perl.eval("3+3")
# Eval can also return functions
sum = perl.eval("sub { my $s = shift; $s += shift while @_; $s }")
print sum(1,2,3)
http://aspn.activestate.com/ASPN/CodeDoc/pyperl/perlmodule.html
but speaking of quoted code and all...
More information about the Python-list
mailing list