Getting in to metaprogramming

Rafe rafesacks at gmail.com
Thu Nov 27 00:11:47 EST 2008


On Nov 27, 11:41 am, "Hendrik van Rooyen" <m... at microcorp.co.za>
wrote:
>  "Steven D'Aprano" <steau> wrote:
>
>
>
> > Well, I don't know about "any problem". And it's not so much about
> > whether metaprograms can solve problems that can't be solved by anything
> > else, as whether metaprograms can solve problems more effectively than
> > other techniques.
>
> > If you include factory functions, class factories, the builder design
> > pattern, metaclasses, etc. as "metaprogramming", then I use it all the
> > time, and find it an excellent technique to use.
>
> > But if you mean using a Python program to generate Python source code,
> > then I can't think of any time I used it. Which doesn't mean that others
> > don't find it helpful, only that I haven't yet.
>
> 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)
>
> Or can I claim a new a new meta - rule - I would call it van Rooyen's folly...
>
>
>
> > 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?
>
>
>
> > I don't think metaprogramming in the limited sense (programs to output
> > source code) is a bad idea, but I do think that there are probably better
> > alternatives in a language like Python.
>
> True. No argument here - I was just wondering if the relationship holds.
>
> - Hendrik


"Given that, can anybody think of an example that you could not do
with a class?"

Generating a template for a specific script application. For example,
a script with pre-defined callbacks that only require the addition of
the contents.

I was really interested in exploring the idea of using python output,
instead of XML, to record something a user did in a GUI. I have seen
it done and it is really advantageous in the 3D industry because it
means the script files can be edited directly, in a pinch, to generate
something slightly different.

For example, say we have code which generates a cube by plotting it's
points. A user then changes a point position in the GUI. The change is
saved by outputting the function call to a file with new arguments
(the new point location). If I wanted to make 100s of copies of the
cube, but with a slightly different point position, I could edit the
custom cube's python code and hand it back for creation without using
the GUI. I could do this with XML, but it can be harder to work with
in a text editor (though I have seen some XML editors that make it a
bit easier.) In fact, in most 3D applications, the app prints
everything the user does to a log. Sometimes in a choice of languages,
so I guess I am looking to do the same thing with my own custom tools.

In a real situation the generated code file can build some pretty
complex 3D object hierarchies. It moves beyond simple storage of data
and becomes a real script that can be hacked as necessary.

It is nice to have everything as python scripts because we always have
a blend of GUI users and developers to get a job done.

- Rafe



More information about the Python-list mailing list