PEP 312 (and thus 308) implemented with a black magic trick

Michele Simionato mis6 at pitt.edu
Tue Mar 18 10:50:17 EST 2003


Few months ago there was a debate on c.l.p. about adding macros to Python [1].
Then everybody started pointing out that macros are evil because they
allow the programmamer to change the *syntax* of the language.

With this example I wanted to point out (using the form of a joke and of 
a clever [2] hack) that we already have metaclasses, that potentially are 
*much more evil* than macros. Metaclasses allow to change the *semantics* of 
the language (WYSINWYG: what you see is not what you get).
Changing the source code on the fly is only a particular way of
changing the semantics, maybe more evidently dirtier than others.

The problem (or advantage, depending on your point of view) is that
there are many other ways of changing the semantics: imagine a metaclass
that wraps all the methods of a class with arbitrary functions and all
the attributes with properties involving arbitrary functions: the
final effect is that you read some code and Python actually sees a
completely different thing.

I remember Peter Hansen complaining about properties, because you could
write an apparently innocuos statement like 'c.x=1' and have any kind
of side effects, including activating a nuclear weapon (of something like
that, IIRC, it was some time ago ;). There are plenty of ways of doing 
such dirty tricks in Python 2.2+, much more than in previous versions. [3]

Fortunately, the dark side of Python is well covered under a clean surface, 
and you could use the language for a long time never suspecting its existence. 
Also, there is little risk that the average programmer would discover all 
the secrets of metaclasses (maybe ill documented on purpose ;)

Nevertheless, the post was meant to say:

 ATTENTION! ATTENTION!

plain, usual, apparently innocuos standard Python already contains a fair
amount of black magic, so much, in effect, that it is possible to implement
PEP 312 and 308 in 20 lines! (there are some limitations, actually, but still 
the 20 line implementation would work for most code).

Python is a very strange language: on the surface it is very constraining
(you are even forced to indent you code!), but under the hood it gives to the
programmer an incredible amount of freedom.

I think it is extremely difficult to figure out all the deepest consequences 
of the new Python object model, and all its possible outcomes. For the
moment, I content myself with exploring, learning and *not* applying :)

                                                   Michele

Notes:

[1] I am not saying I want macros added to Python
[2] Clever is not the same than wise, as Laura Chreighton 
    pointed out some time ago.
[3] I am not saying I want these controversial features removed.

P.S.

"Raymond Hettinger" <vze4rx4y at verizon.net> wrote in message news:<wuuda.50506$68.36489 at nwrdny01.gnilink.net>...
> Dear Dr. Strangelove,
> 
> You are a dangerous man.
> I'm glad you're on our side.
> You are on our side, aren't you?
> 
> curiouser-and-curiouser-ly yours,
> 
> 
> Raymond Hettinger

I am the kind of person who likes more Darth Vader than Luke 
Skywalker ... ;)




More information about the Python-list mailing list