Python Forum

Full Version: a funny coding challege
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
write a script that is to be run as a command under either version of python and run itself under the other version and print "hello" the run itself again under the original version and print "world" and stop.
#!/usr/bin/env python2
from __future__ import print_function
import subprocess

print(subprocess.check_output(['/usr/bin/env', 'python3', '-c', 'print("Hello", end="")']), 'World')
that's not running itself, it's running a command string.  "itself" means the file, not a command string, and not some other file.  running itself is to run the script file.  if i run the script under python3 then it should run itself under python2, and while running under python2, print "hello", then run itself under python3, and while running under python3, print "world", then do nothing more.  if i run the script under python2 then it should run itself under python3, and while running under python3, print "hello", then run itself under python2, and while running under python2, print "world", then do nothing more.