Python Forum

Full Version: 2to3 fails on very simple script
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3
i have this very simple script:
print()
that prints
Output:
()
but when i run 2to3 on it, it says

Output:
RefactoringTool: No files need to be modified.
D'oh!
It is 2 to 3, you attempt to modify 3 to what?   Wink
(Oct-18-2016, 10:14 AM)wavic Wrote: [ -> ]It is 2 to 3, you attempt to modify 3 to what?   Wink

well, he "attempts" to convert simple python 2 script that has single print statement that prints empty tuple...
I didn't try this myself, but I assume Skaperen did it right that's the result he gets. Anyway - from the docs:
Quote:Since some print statements can be parsed as function calls or statements, 2to3 cannot always read files containing the print function.
(Oct-18-2016, 10:38 AM)buran Wrote: [ -> ]well, he "attempts" to convert simple python 2 script that has single print statement that prints empty tuple...
Hm! It looks like python3 print function to me? May be it's what you say.
Exactly. That single line is legitimate in both python 2 (as a print statement) and in python 3 (as a print function). And the docs make it clear in this case 2to3 is not to be blamed
That's already a valid python3 file. So there isn't any conversion that needs to take place.
(Oct-18-2016, 03:04 PM)nilamo Wrote: [ -> ]That's already a valid python3 file.  So there isn't any conversion that needs to take place.

in python 2 it is a print statement that prints an empty tuple Tongue   so what if python3 can run it as-is Sick  it does something different with it (prints an empty line) Shy   no i did not read the docs of 2to3 so i was not aware of cases like this that it cannot figure out Evil   let's see if you can write python 3 code that does what this code does in python 2 (it prints an empty tuple) Confused   would you cheat and use the string '()' Huh  i might Big Grin
In 2to3 doc.

Quote:When the -p is passed, 2to3 treats print as a function instead of a statement.

You can of course also put this line in python 2.x code.
from __future__ import print_function

print()
true, but that's not the challenge. the challenge is to get code that does the same thing now in python3 and keep it simple. how simple can it be done in python3?  is this the best case?
print('()')
?
(Oct-19-2016, 06:51 AM)Skaperen Wrote: [ -> ]true, but that's not the challenge. the challenge is to get code that does the same thing now in python3 and keep it simple. how simple can it be done in python3?  is this the best case?
print('()')
?

well, I start to be confused about all this. If you want the python 3 code to print empty tuple, then it is as follows
print(())
Output:
Python 3.5.2 (default, Jul 17 2016, 00:00:00) [GCC 4.8.4] on linux Type "help", "copyright", "credits" or "license" for more information. >>> print (()) () >>>
I don't understand why the complications with printing the  str '()'. If you want to print empty tuple, do it...
The original questions was regarding unexpected behaviour using 2to3 tool
Pages: 1 2 3