Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Favorite non-python language trick?

Reply
Thread Tools

Re: Favorite non-python language trick?

 
 
Steve
Guest
Posts: n/a
 
      06-25-2005
Hi,

> I'm curious -- what is everyone's favorite trick from a non-python
> language? And -- why isn't it in Python?


One thing that I miss every once in a while is "pre-processing". I'm
wondering if I'm the only one here, since nobody seems to have brought
that up. For example, after testing is done and I need to release some
bit of code in production, I would like create the .pyc/.pyo, like so:

# pycompile -DMAX_FOO=100 -D__version__=1.0 -D__release__=1.stable foo_module.py

where pycompile, is an home baked script that maybe uses compile/compileall..

I know, I can do the preprocessing myself in pycompile, but this would
be a nice to have.

Regards
Steve



On 6/24/05, Joseph Garvin <(E-Mail Removed)> wrote:
> As someone who learned C first, when I came to Python everytime I read
> about a new feature it was like, "Whoa! I can do that?!" Slicing, dir(),
> getattr/setattr, the % operator, all of this was very different from C.

<...snip...>
 
Reply With Quote
 
 
 
 
Roy Smith
Guest
Posts: n/a
 
      06-25-2005
Steve <(E-Mail Removed)> 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-Computerlay$ cat pp.h
#define foo(bar) mySillyFunction(bar, 0)

def mySillyFunction(x, y):
x / y
-------------------------------

and

-------------------------------
Roy-Smiths-Computerlay$ cat pp.pre
#include "pp.h"

foo(37)
-------------------------------

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-Computerlay$ 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Favorite non-python language trick? Joseph Garvin Python 138 07-07-2005 07:36 PM
OFF-TOPIC:: Why Lisp is not my favorite programming language nobody Java 28 04-15-2004 02:18 PM
OFF-TOPIC:: Why Lisp is not my favorite programming language nobody C Programming 26 04-15-2004 02:18 PM
OFF-TOPIC:: Why Lisp is not my favorite programming language nobody Python 39 04-15-2004 02:18 PM
OFF-TOPIC:: Why Lisp is not my favorite programming language nobody C++ 26 04-15-2004 02:18 PM



Advertisments