Favorite non-python language trick?

Roy Smith roy at panix.com
Sat Jun 25 15:45:14 CEST 2005

Steve <lonetwin at gmail.com> wrote:
> One thing that I miss every once in a while is "pre-processing".

There are so many things wrong with preprocessing, at least if you're 
thinking of the way C/C++ implements it (cpp).  Cpp a number of things.

1) It does file inclusion.  Pythons "import" serves that purpose and does a 
better job of it.

2) It defines constants.  A simple assignment in Python does that just fine 
(and in C++, enums and const variables do it).

3) It does conditional compilation.  Just use "if" in Python.  You can even 
enclose function definitions inside an "if" body.

4) It lets you do macro expansion.  Just write a function in Python.

The big problem with cpp is that it essentially lets you define a whole new 
language.  The code that is compiled/executed isn't anything like the code 
that you see on the screen.  This can lead to debugging nightmares.

BTW, if you *really* want to, you can use cpp with Python.  For example, I 
just wrote the following little program.  It's in a language I'll call 
Prethon, for lack of a better name.  It comes in two files:

Roy-Smiths-Computer:play$ cat pp.h
#define foo(bar) mySillyFunction(bar, 0)

def mySillyFunction(x, y):
    x / y


Roy-Smiths-Computer:play$ cat pp.pre
#include "pp.h"


I can use cpp to turn this into a Python program by doing "cpp pp.pre > 
pp.py".  When I run that, I get a run-time exception and a stack trace:

Roy-Smiths-Computer:play$ python pp.py
Traceback (most recent call last):
  File "pp.py", line 13, in ?
    mySillyFunction(37, 0)
  File "pp.py", line 10, in mySillyFunction
    x / y
ZeroDivisionError: integer division or modulo by zero

This is really confusing.  I'm looking at my source code and don't see any 
calls to mySillyFunction().  In the trivial example I've given, it's 
obvious that it's a #define in pp.h, but in a real-life example, there can 
be hundreds or thousands of included files in a project, scattered about 
all sorts of different directories you may not even know exist.  Trying to 
figure out what's going on becomes a nightmare.

More information about the Python-list mailing list