An earlier post today got me thinking about "quines" (programs that
output themselves) in Python. I tried to find some on the web but
didn't find many ([1]). In particular I didn't find any that
corresponded to my instinctive (LISP-induced, probably) criterion:
def self_evaluating(s):
"Return True if string s evaluates to itself"
return s == eval(s)
Here is the result of my efforts so far:
1. Starting from the classic idea (lambda x:x%x)('lambda x:x%x') I got
the following
v=(lambda x:x%('"''""'+x+'"''""'))("""(lambda x:x%%('"''""'+x+'"''""'))
(%s)""")
2. (Given that if s is a nonempty string, s*2 is a longer string).
Starting from the idea "%s %s" % (("%s %s",)*2) I got the following
u="\"%s\" %% ((r\"%s\",)*2)" % ((r"\"%s\" %% ((r\"%s\",)*2)",)*2)
Most of my problems in creating these 2 was with finding a suitable
way of quoting strings that propagates well. Both u and v are one-
liners. I'm hoping for no funky line wrapping here.
Note: I'm not quoting the string as it makes no difference since they
evaluate to themselves:)
I'd like to know if anyone on the list has indulged in this time-
bending/mind-wasting activity before. If so, it would be nice to
create a list of such expressions.
Quining's-better-than-ironing'ly yours
--
Arnaud
>>> from inspect import getsource
>>> print getsource(getsource)
or similarly
def f(g):
import inspect
return inspect.getsource(g)
print f(f)
Dan
>>> print x
Traceback (most recent call last):
File "<pyshell#15>", line 1, in <module>
eval(x)
File "<string>", line 2
Traceback (most recent call last):
^
SyntaxError: invalid syntax
>>> eval(x)
Traceback (most recent call last):
File "<pyshell#15>", line 1, in <module>
eval(x)
File "<string>", line 2
Traceback (most recent call last):
^
SyntaxError: invalid syntax
>>>
Arnaud Delobelle wrote:
> Hi all,
>
> An earlier post today got me thinking about "quines" (programs that
> output themselves) in Python. I tried to find some on the web but
> didn't find many ([1]). In particular I didn't find any that
> corresponded to my instinctive (LISP-induced, probably) criterion:
>
> def self_evaluating(s):
> "Return True if string s evaluates to itself"
> return s == eval(s)
>
> Here is the result of my efforts so far:
>
> 1. Starting from the classic idea (lambda x:x%x)('lambda x:x%x') I got
> the following
> v=(lambda x:x%('"''""'+x+'"''""'))("""(lambda
> x:x%%('"''""'+x+'"''""'))(%s)""")
>
> 2. (Given that if s is a nonempty string, s*2 is a longer string).
> Starting from the idea "%s %s" % (("%s %s",)*2) I got the following
> u="\"%s\" %% ((r\"%s\",)*2)" % ((r"\"%s\" %% ((r\"%s\",)*2)",)*2)
>
> Most of my problems in creating these 2 was with finding a suitable way
> of quoting strings that propagates well. Both u and v are one-liners.
> I'm hoping for no funky line wrapping here.
>
> Note: I'm not quoting the string as it makes no difference since they
> evaluate to themselves:)
>
> I'd like to know if anyone on the list has indulged in this
> time-bending/mind-wasting activity before. If so, it would be nice to
Some years ago there was a substantial thread on this, (Shortest
Self-Reproducing Programs, or some such) including a fairly long one from
me that gave several 'shortest' (depending of definition and input method).
It included a Python tranlation of at least one standard Lisp version. I
presume you could find it on groups.google
Terry J. Reedy
A bit more readable:
v1=(lambda x:x%('r\"'+x+'\"'))(r"(lambda x:x%%('r\"'+x+'\"'))(%s)")
To Dan and Boris, this is cheating:-) I particularly like Boris's
"solution", it bends the rule very nicely! Although you should get a
NameError, not a SyntaxError for python < 3.0.
--
Arnaud
> Some years ago there was a substantial thread on this, (Shortest
> Self-Reproducing Programs, or some such) including a fairly long one from
> me that gave several 'shortest' (depending of definition and input method).
> It included a Python tranlation of at least one standard Lisp version. I
> presume you could find it on groups.google
It contains a lambda-solution similar to mine, only more concise:
(lambda x:x%`x`)('(lambda x:x%%`x`)(%s)')
I have been using python for 7 years, and it the first time ever that
I see the `...` construct being used! In fact I didn't even remember
that it existed (I'm not even sure that I've ever known about it...)
Thanks
--
Arnaud
Exactly the one I meant.
| It contains a lambda-solution similar to mine, only more concise:
| (lambda x:x%`x`)('(lambda x:x%%`x`)(%s)')
Being a Lisp novice, I was rather proud of that translation
| I have been using python for 7 years, and it the first time ever that
| I see the `...` construct being used!
It is going away in 3.0, so the above would need revision to work with
repr(), if indeed it will.
| Thanks
You're welcome
Terry
Here's the py3k-compliant version:
>>> k=(lambda x:x%repr(x))('(lambda x:x%%repr(x))(%s)')
>>> k == eval(k)
True
--
Arnaud
--- probably_not_a_real_quine.py ----
import sys
for line in open(sys.argv[0]):
print line,
--------------------------------------
;-)
--
Ant.