Getting in to metaprogramming

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Wed Nov 26 09:51:06 EST 2008


On Thu, 27 Nov 2008 00:55:33 +0200, Hendrik van Rooyen wrote:

> "Aaron Brady" <castironpi at gmail.com> wrote:
> 
> 
>>I don't know a clean, reliable way to structure a metaprogram though.
>>Mine always turn out messy.
> 
> Yes.
> 
> Then another thing - it strikes me that any problem that can be solved
> by metaprogramming, can be solved by putting similar code into a class
> and instanciating an instance.
> 
> Does anybody know if this is true?

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.

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.

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.


-- 
Steven



More information about the Python-list mailing list