Python Forum
a funny coding challege
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
a funny coding challege
#1
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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
#!/usr/bin/env python2
from __future__ import print_function
import subprocess

print(subprocess.check_output(['/usr/bin/env', 'python3', '-c', 'print("Hello", end="")']), 'World')
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  a funny code nzcan 0 2,057 Oct-31-2018, 08:51 PM
Last Post: nzcan
  Funny English Kebap 41 23,911 Feb-10-2018, 02:06 AM
Last Post: Skaperen
  Funny things Java Devs do but Python Devs Don't QueenSvetlana 1 3,266 Nov-02-2017, 08:45 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020