Created
October 12, 2018 12:22
-
-
Save pfuntner/2e48a4e96988d18e3533a176a23376f6 to your computer and use it in GitHub Desktop.
Test Python print function call vs statement
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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)') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment