Python #ifdef

Dave Angel davea at davea.name
Tue May 28 15:57:49 EDT 2013


On 05/28/2013 03:46 PM, Carlos Nepomuceno wrote:
> Are there Python 'preprocessor directives'?

Python doesn't define a preprocessor, and CPYthon doesn't implement one. 
  Nothing to stop you from doing so, however.
>
> I'd like to have something like '#ifdef' to mix code from Python 2 and 3 in a single file.
>
> Is that possible? How? 		 	   		
>

It's quite possible that you don't need a preprocessor do what you want, 
since Python does much less compile-time checking than C.  Also, it's 
possible to store function objects and module objects in variables, and 
thus to hide many things from the body of the code.

One biggie is print, since that's a reserved word (and a statement) in 
Python 2.x.  But if you're using 2.7 you can use from __future__ import 
print, or something like that, and just use 3.x function semantics.


-- 
DaveA



More information about the Python-list mailing list