1 |
Am Mon, 17 Sep 2012 23:27:34 +0100 |
2 |
schrieb David W Noon <dwnoon@××××××××.com>: |
3 |
|
4 |
> On Mon, 17 Sep 2012 17:17:47 -0400, Philip Webb wrote about Re: |
5 |
> [gentoo-user] new machine : suddenly Python 3 appears: |
6 |
> |
7 |
> > 120917 Philip Webb wrote: |
8 |
> > > 120917 David W Noon wrote: |
9 |
> > >> print(' {0}'.format(eval(expression))) |
10 |
> > |
11 |
> > That works properly with Python2 in this machine ; |
12 |
> > I'll check it with Python3 later in the new machine. |
13 |
> |
14 |
> That *is* Python 3 syntax. It is also accepted under recent releases |
15 |
> of Python 2. |
16 |
> |
17 |
> > > 120917 Marc Joliet wrote: |
18 |
> > >> print(' ',eval(expression)) |
19 |
> > |
20 |
> > That does the calculation, but the output is wrongly formatted : |
21 |
> > |
22 |
> > 514 bin> pycalc1 2+3 |
23 |
> > (' ', 5) |
24 |
> |
25 |
> This is because Marc's code is syntactically invalid for Python 3. It |
26 |
> is acceptable to Python 2, but does not do what you want; but it won't |
27 |
> work at all under Python 3. |
28 |
|
29 |
No, he simply executed it using python2, hence he printed a tuple. My version |
30 |
certainly prints the exact same output as the python2 version, *when executed |
31 |
by python3*. You tried it out first, right? I certainly did. |
32 |
|
33 |
Note that the only difference to your version is that yours does string |
34 |
formatting via str.format(), which also exists in Python 2. That of course has |
35 |
the advantage of working the same in Python 2 and 3 (*if* you care about that): |
36 |
the argument to print is not a tuple anymore, so it is not formatted as such: |
37 |
|
38 |
$ python3 -c "print('{0}'.format(3))" |
39 |
3 |
40 |
$ python2 -c "print('{0}'.format(3))" |
41 |
3 |
42 |
|
43 |
> It is clear that you have not taken my advice to use the -3 run-time |
44 |
> option in your hash-bang line. At the risk of blowing my own trumpet, |
45 |
> that was *extremely sound* advice; you should really take it. It would |
46 |
> have revealed the problems with the above code during the Python |
47 |
> interpreter's initial scan of the code. I'll repeat it: |
48 |
> !#/usr/bin/python2 -3 |
49 |
> This will perform a Python 3 syntax check, even under Python 2. It |
50 |
> will identify any going-forward issues for your Python script(s). |
51 |
|
52 |
I also just remembered the "2to3" program, which will translate trivial (such |
53 |
as print vs. print()) and (I think) some not so trivial cases to python3 syntax |
54 |
for you. Personally I think you should just convert your scripts to python3 and |
55 |
be done with it, unless you want to avoid python3 forever. |
56 |
|
57 |
Of course, Philip did say this is his only python script, so using "python2 -3" |
58 |
might actually be a tad overkill for his purposes, unless he wants to start |
59 |
learning python, in which case: why not just start with python 3? |
60 |
|
61 |
HTH |
62 |
-- |
63 |
Marc Joliet |
64 |
-- |
65 |
"People who think they know everything really annoy those of us who know we |
66 |
don't" - Bjarne Stroustrup |