Easy questions from a python beginner

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Fri Jul 23 05:05:21 EDT 2010


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.

If you have a class that defines a single member (or attribute in Python 
terminology) "spam", and you want to add a second "ham" to a specific 
instance, such a thing is either deep, dark metaprogramming in some 
languages, if not outright impossible. In Python it is not even 
noteworthy:

instance.ham = "something"  # *yawns*

Recently there was a thread started by some Java or C++ refugee who was 
distressed about attribute access in Python, because it made 
metaprogramming frighteningly easy:

http://mail.python.org/pipermail/python-list/2010-June/1248029.html

My response at the time was: Python makes metaprogramming *easy*:

http://mail.python.org/pipermail/python-list/2010-June/1248053.html

I can't imagine how he would have reacted if we had showed him how easy 
it is to monkey-patch built-in functions...

[...]
> But! What it doesn't let you do is get clever with syntax and write a
> sort of "simplified" or domain specific language to achieve certain
> sorts of repetitive tasks quickly. You always end up writing normal
> Python that looks like all other Python.

Exactly... 90% of the time that you think you want a DSL, Python beat you 
to it.

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.


-- 
Steven



More information about the Python-list mailing list