selfrepr.py for your amusement ;-)

Bengt Richter bokr at oz.net
Wed Jan 30 03:23:47 CET 2002


# selfrepr.py -- to demo self-reproducing (not shortest ;-) code -- bokr 2002
# python selfrepr.y > clone.py should result in clone which can also reproduce.
nfront=5
def pq(k): print '  '+chr(34)+k+chr(34)+','
t=[ # this is last line of t[:nfront] -- define nfront value accordingly
  "# selfrepr.py -- to demo self-reproducing (not shortest ;-) code -- bokr 2002",
  "# python selfrepr.y > clone.py should result in clone which can also reproduce.",
  "nfront=5",
  "def pq(k): print '  '+chr(34)+k+chr(34)+','",
  "t=[ # this is last line of t[:nfront] -- define nfront value accordingly",
  "] # this is first line of t[nfront:]",
  "import sys; sys.stderr.write(chr(7)) # put any non-stdout tricks here ;-)",
  "# the following reproduces everything",
  "for x in t[:nfront]:",
  "  print x",
  "for x in t:",
  "  pq(x)",
  "for x in t[nfront:]:",
  "  print x",
  "# you could put extra stuff here too, just dupe it as part of t[nfront:] quoted.",
] # this is first line of t[nfront:]
import sys; sys.stderr.write(chr(7)) # put any non-stdout tricks here ;-)
# the following reproduces everything
for x in t[:nfront]:
  print x
for x in t:
  pq(x)
for x in t[nfront:]:
  print x
# you could put extra stuff here too, just dupe it as part of t[nfront:] quoted.




More information about the Python-list mailing list