In article <[EMAIL PROTECTED]>, Duncan Booth <[EMAIL PROTECTED]> wrote: >"[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote: > >> I'm trying to use the gpp utility (Gnu points to >> http://en.nothingisreal.com/wiki/GPP) to do conditional compilation in >> Python, and I'm running into a problem: the same '#' character >> introduces Python comments and is used by default to introduce #ifdef >> etc. lines. >> >> Here's an example of what I'm trying to do: >> >> #ifdef DEBUG >> stderr.write("variable is...") #details of msg omitted >> #endif >> > >Why do you want conditional compilation. Is there anything wrong with: > > if __debug__: > stderr.write("variable is...") #details of msg omitted > >If you run Python with the -O command line option the code protected by the >if statement will be optimised out. > >For most other purposes where you might use conditional compilation just >adding 'if' statements to execute at runtime (or try/except) will do the >same purpose: > >if sys.platform=='win32': > def foo(): > ... something ... >else: > def foo(): > .... something different ...
I want to reinforce this. Yes, it is possible to pre-process Python source, it's even been done before, and I'm sympathetic to the suggestion that m4's more appropriate than gpp. However, Duncan and others who've followed up are right: you can live without this stuff. In fact, those who think they need condi- tional compilation with Python are, with very few exceptions, simply mistaken. The urge to transform Python source this way almost always is a symptom of unfamiliarity with Python potential and good style. It's not just that Python can do without conditional compilation; Python offers *better* means to achieve the same goals. -- http://mail.python.org/mailman/listinfo/python-list