Re: Favorite non-python language trick?

Discussion in 'Python' started by Steve, Jun 25, 2005.

  1. Steve

    Steve Guest

    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 <> 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...>
     
    Steve, Jun 25, 2005
    #1
    1. Advertising

  2. Steve

    Roy Smith Guest

    Steve <> 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
    -------------------------------

    and

    -------------------------------
    Roy-Smiths-Computer:play$ 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-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.
     
    Roy Smith, Jun 25, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. nobody
    Replies:
    28
    Views:
    806
    Mike Nishizawa
    Apr 15, 2004
  2. Joseph Garvin

    Favorite non-python language trick?

    Joseph Garvin, Jun 24, 2005, in forum: Python
    Replies:
    138
    Views:
    2,068
    Robert Kern
    Jul 7, 2005
  3. Jp Calderone

    Re: Favorite non-python language trick?

    Jp Calderone, Jul 1, 2005, in forum: Python
    Replies:
    1
    Views:
    337
    Rocco Moretti
    Jul 3, 2005
  4. Replies:
    2
    Views:
    379
  5. Steve Holden
    Replies:
    0
    Views:
    257
    Steve Holden
    May 1, 2008
Loading...

Share This Page