Please translate this easy snip of C++ to Python

Phlip phlip_cpp at my-deja.com
Mon Feb 5 06:59:47 CET 2001


Fredrik Lundh wrote:

> That's a preprocessor trick, not a C++ trick.  Replace <<
> with printf, and it works just as fine under C.

Could'a mentioned that's where the trick started with me. Before the # 
stringerizer operator got invented, too...

> (iirc, bjarne thinks that real C++ programmers shouldn't
> use the preprocessor at all, but what does he know...)

The exact rule is "Don't use a low-tech feature when a high-tech one will 
do". You'l notice this rule explains why I'm deep in the coils of Python 
these days...

One should not use the C preprocessor to perform simple administration, 
such as declaring a constant. But only the preprocessor has the 
stringerizer operator #; and it's a quite recent addition, based on bugs 
exploited in earlier preprocessors to stringerize without it, so it 
qualifies as high-tech.

> Run your Python code through C's preprocessor?

We are already deep in a stack of script, Perforce Jam, Python, SWIG and 
C++ code; we don't need another operation on our source!

Thomas Wouters wrote:

> Note how Python lacks a preprocessor and macros :)

No, reaching down into the interpret's own pants and grabbing its nads with 
lines like these will do nicely:

        frame = sys.exc_info()[2].tb_frame.f_back

chris at onca.catsden.net wrote:

> And... not sure if there's a equivalent of __line__

I just threw that one in worrying the first question might turn out too 
easy...

Thanks to all the replies; I'l try the suggestions (and learn what 
"trace()" does) as soon as I'm on the clock ;-)

-- 
  Phlip                          phlip_cpp at my-deja.com
============ http://c2.com/cgi/wiki?PhlIp ============
  --  Who needs a degree when we have Web e-search engines?  --



More information about the Python-list mailing list