Easy questions from a python beginner

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Fri Jul 23 22:44:15 EDT 2010


On Fri, 23 Jul 2010 09:22:16 -0700, Stephen Hansen wrote:

> On 7/23/10 2:05 AM, Steven D'Aprano wrote:
>> On Thu, 22 Jul 2010 21:23:05 -0700, Stephen Hansen wrote:
>> 
>>> On 7/22/10 7:47 PM, wheres pythonmonks wrote:
>> [...]
>>>> The truth is that I don't intend to use these approaches in anything
>>>> serious.  However, I've been known to do some metaprogramming from
>>>> time to time.
>>>
>>> Depending on how you define "metaprogramming", Python is pretty
>>> notoriously ill-suited towards the task (more, its basically
>>> considered a feature it doesn't let you go there) -- or, it allows you
>>> to do vast amounts of stuff with its few dark magic hooks. I've never
>>> had a satisfying definition of metaprogramming that more then 50% of
>>> any group agree with, so I'm not sure which you're looking for. :)
>> 
>> I disagree strongly at your characterisation that Python is notorious
>> for being ill-suited towards metaprogramming. I'd say the complete
>> opposite -- what is considered dark and scary metaprogramming tasks in
>> other languages is considered too ordinary to even mention in Python.
> 
> I rather think you missed my point entirely.
> 
> 'Depending on how you define "metaprogramming"'
> 
> The 'Depending' was the most important part of that sentence. 

Well, I suppose some folks might like to define words any way they like, 
so that "cat" means a reptile with no legs and "lunch time" means the 
thing you do in the shower, but I prefer to stick with common 
definitions. And the common definition of metaprogramming is that it is 
"programming about programming" -- programming where the data you are 
processing is itself programming code and data structures.

http://en.wikipedia.org/wiki/Metaprogramming

I did not miss your proviso, nor did I miss the fact that there are some 
metaprogramming techniques that Python is not very good at, such as 
runtime modification of syntax. I wrote:

"It is true that other languages (like Lisp, or Forth) allow you to write 
your own syntax. That's incredibly powerful, but it also has serious 
costs. If you can define your own syntax, that means every piece of code 
you look at is potentially a different language."

But what makes you think that the OP is trying to do that? There's 
nothing in his comments to indicate that to me. Perhaps I missed 
something.



> You go on
> to talk about runtime modification of classes and the like. That, Python
> lets you do quite readily, and then go on to do crazy amounts of without
> any significant effort.
> 
> That's one definition of metaprogramming. That one Python does well.

No, it's not a *definition* of metaprogramming. It is an aspect of 
metaprogramming, in the same way that roasting food is not a definition 
of cooking, it is one out of many cooking techniques.


> The other involves things like macros, or things where you basically
> write a new sub-language in the language itself to achieve some commonly
> desired task more efficiently (or just more succinctly). That's another
> definition of metaprogramming: where its not so much structures
> (classes, etc) which are modified at runtime (which Python lets you do
> readily), but the syntax or semantics of the language itself. That
> Python isn't game for.

You can't modify Python's syntax at runtime, but you can create DSLs in 
Python quite readily provided you accept the limitation of Python's 
syntax. There was even a tutorial at PyCon about it:

http://us.pycon.org/2010/tutorials/jensen_languages/

Although he doesn't use the term "DSL" anywhere in his essay, Guido's 
essay on creating graphs in Python essentially describes a DSL for graphs:

http://www.python.org/doc/essays/graphs/

In the Python Cookbook, Paul Dubois also gives an "application-specific-
language" for graphs in a section called "Using Python Itself as a Little 
Language". Yes, it's limited to Python syntax, but it's still a DSL.

And of course there are applications like Cheetah:

http://www.cheetahtemplate.org/

Python doesn't go all the way like Forth or Lisp, but there aren't very 
many metaprogramming techniques you can't do it in.



-- 
Steven



More information about the Python-list mailing list