Python #ifdef

Terry Jan Reedy tjreedy at udel.edu
Tue May 28 20:31:39 EDT 2013


On 5/28/2013 6:25 PM, Joel Goldstick wrote:
> On Tue, May 28, 2013 at 6:18 PM, Mark Lawrence <breamoreboy at yahoo.co.uk
> <mailto:breamoreboy at yahoo.co.uk>> wrote:

>     On 28/05/2013 20:46, Carlos Nepomuceno wrote:
>         I'd like to have something like '#ifdef' to mix code from Python
>         2 and 3 in a single file.

>     https://pypi.python.org/pypi/__six/1.3.0
>     <https://pypi.python.org/pypi/six/1.3.0>

> my original response was from cell phone.  I just answered that you
> can't do ifdefs, implying that there is no preprocessor in python.  I
> learned a lot of things I didn't know reading the thread, but I wonder
> if it is a good idea in general to try to write code like this.  --
> combined 2.x/3.x codebase can be a bear to maintain.

Many people have come to prefer a) a single 2&3 codebase over b) 
separate 2 and 3 codebases or c) a single 2 codebase repeatedly 
converted to a 3 codebase with 2to3.

They use 2to3 once (well, a few times) to discover differences that need 
to be considered.

For 2.7 and 3.x, the future imports are enough for some code. The six 
module handles harder cases.

> I wouldn't do it
> unless there was some imposing reason that I must.  Its not just print()
> -- that isn't bad, but changes in module names (urllib),

I believe six handles that

 > arithmetic, and

from __future__ import integer_division  # spelling?
handles the only change

> unicode

Use unicode consistently and
from __future__ import unicode_literals  # spelling?
or the re-addition u'' prefix do quite well.

Otherwise, do not use things that evaporated, like apply() and classic 
classes. (Inherit from object if nothing else.)

This is all hearsay coming from me ;-).

Terry





More information about the Python-list mailing list