version

Dan Stromberg drsalists at gmail.com
Fri Jun 1 23:59:07 EDT 2018


On Fri, Jun 1, 2018 at 6:34 PM, Mike McClain <mike.junk.46 at att.net> wrote:

> On Fri, Jun 01, 2018 at 08:02:27AM -0700, Mike McClain wrote:
> > On Thu, May 31, 2018 at 07:44:35PM -0700, Mike McClain wrote:
> > <snip>
> > > Is there a way in a script to know which version of python is being
> > > run so I can write:
> > >     If (version == 2.7):
> > >         do it this way
> > >     elsif (version == 3.2):
> > >         do it another way
>


> I looked at 'six' and it putting wrappers around calls would mean a
> rewrite of what is basically just a playground for exploring what
> python is and certainly not worth the trouble to rewrite much.
>
> It looks like what I was wanting is something like 'C's #if, a
> compiler conditional.
>
> Does python have anything like that to tell the interpreter to ignore
> a line that is not a comment or a quoted string?
>

I've had some luck using m4 for preprocessing Python and Cython from the
same .py file.  However, it's often better to have 2 different files, as
evidenced by some of C's #ifdef'd files that get really complicated and
hard to test.

You're probably better off using 3.x at this point.

BTW, if you just want a simplistic way of doing print's that works on 2.x
and 3.x, there are some options:
1) sys.stdout.write(string)
2) print(string)
3) from __future__ import print_function

#2 is a bit of a trick/shenanigan that can be useful; it works because for
single-argument print's, the print function and the print statement both
work with parentheses.  You can even:

print('abc %s def %s' % ('123', 456))

On both.  Two 2.x, that's a parenthesized string passed to the print
statement, and to 3.x it's a single-argument print function.

HTH



More information about the Python-list mailing list