Disclaimer
I will be upfront and say I think Python 3 made a terrible mistake by making is so that Python 2 scripts would no longer work. Most of my experience has been with Python 2 and I'm used to using the print statement to print a line to stdout. I hate that Python 3 makes you call print as a function. Yes, I know there are other differences and they're threatening to stop supporting Python 2 starting 2020/01/01. I don't know what I'll do after that point. I suppose Python 2 will still be around but it won't be improved and bugs will not be fixed and I may be ok with that.
What can you deduce from print(expression)?
I've started a new job, looking at new code, and was without knowing much about the execution environment, I was beginning to believe that they must be using Python 3 because I saw something like the following in a Python script:
print(expression)This seemed to be a reasonable conclusion. However, I also found code in the Python 2 style that did not use parentheses!!
How can this be? Do they have two different execution environments? I took it upon myself to try a little test to see definitively what each version supported and here's what I came up with:
# There is no pound-hash operator because I intend this script to be called using `py -2 print-test` or `py -3 print-test` | |
import sys | |
def test(stmt): | |
sys.stdout.write('\nTesting {stmt!r}\n'.format(**locals())) | |
try: | |
exec(stmt) | |
except Exception as e: | |
sys.stderr.write('Caught: {e}\n'.format(**locals())) | |
sys.stdout.write('Welcome to Python {version}\n'.format(version=sys.version)) | |
# this will fail from Python 3 because `print` is a function call and there are no parentheses | |
test('print sys.argv') | |
# this will work from Python 2 because the parentheses are essentially ignored. `print` is still executed as a statement (not a function) | |
# and the parentheses just wrap a string. They're not neceessary in this example but they don't hurt but they don't help either. They could | |
# be used to express a tuple but an embedded comma such as `(expr , )` would necessary to accomplish that. | |
test('print(sys.argv)') |
Python 2 behavior
$ py -2 print-test Welcome to Python 2.7.15 (v2.7.15:ca079a3ea3, Apr 30 2018, 16:30:26) [MSC v.1500 64 bit (AMD64)] Testing 'print sys.argv' ['print-test'] Testing 'print(sys.argv)' ['print-test']
Python 3 behavior
$ py -3 print-test Welcome to Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)] Testing 'print sys.argv' Caught: Missing parentheses in call to 'print'. Did you mean print(sys.argv)? (, line 1) Testing 'print(sys.argv)' ['print-test']
Conclusions
As you can see, using parentheses doesn't hurt in Python 2. However they are required with Python 3, of course.
I guess the moral this excursion is: You can determine Python 2 is used if parentheses are not present in a call to print but you can make no determination if parentheses are used since it that will work with either version.
Comments
Post a Comment