Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Python self-evaluating strings

0 views
Skip to first unread message

Arnaud Delobelle

unread,
Jan 27, 2008, 11:14:19 AM1/27/08
to pytho...@python.org
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
create a list of such expressions.

Quining's-better-than-ironing'ly yours

--
Arnaud

[1] http://www.nyx.net/~gthompso/self_pyth.txt

dg.googl...@thesamovar.net

unread,
Jan 27, 2008, 1:47:13 PM1/27/08
to
It's a bit cheap, but how about

>>> from inspect import getsource
>>> print getsource(getsource)

or similarly

def f(g):
import inspect
return inspect.getsource(g)
print f(f)

Dan

Boris Borcic

unread,
Jan 27, 2008, 2:00:18 PM1/27/08
to
Now there's always that style :

>>> 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

Terry Reedy

unread,
Jan 27, 2008, 6:58:48 PM1/27/08
to pytho...@python.org

"Arnaud Delobelle" <arn...@googlemail.com> wrote in message
news:1BEEBF03-1A38-4745...@gmail.com...

| 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:
...

| 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.

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

Arnaud Delobelle

unread,
Jan 27, 2008, 7:07:31 PM1/27/08
to
On Jan 27, 4:14 pm, Arnaud Delobelle <arno...@googlemail.com> wrote:
[...]

> 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)""")

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

Arnaud Delobelle

unread,
Jan 27, 2008, 7:17:18 PM1/27/08
to
On Jan 27, 11:58 pm, "Terry Reedy" <tjre...@udel.edu> wrote:
> "Arnaud Delobelle" <arno...@googlemail.com> wrote in message

> 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

I found this:
http://groups.google.com/group/comp.lang.python/browse_thread/thread/a1316cb4e216eba4/0cda739385abd03c?lnk=gst&q=Self-Reproducing+Program#0cda739385abd03c

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

Terry Reedy

unread,
Jan 28, 2008, 10:48:11 PM1/28/08
to pytho...@python.org

"Arnaud Delobelle" <arn...@googlemail.com> wrote in message
news:a04ca850-fe63-4b7e...@i29g2000prf.googlegroups.com...

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

Arnaud Delobelle

unread,
Jan 29, 2008, 2:06:34 AM1/29/08
to
On Jan 29, 3:48 am, "Terry Reedy" <tjre...@udel.edu> wrote:
> "Arnaud Delobelle" <arno...@googlemail.com> wrote in message
>
> news:a04ca850-fe63-4b7e...@i29g2000prf.googlegroups.com...
> | I found this:http://groups.google.com/group/comp.lang.python/browse_thread/thread/...

>
> 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.

Here's the py3k-compliant version:

>>> k=(lambda x:x%repr(x))('(lambda x:x%%repr(x))(%s)')
>>> k == eval(k)
True

--
Arnaud

Ant

unread,
Jan 29, 2008, 5:00:13 AM1/29/08
to
In the spirit of "Simple is better than complex." and totally
bypassing the intention of quines (though not necessarily the
definition):

--- probably_not_a_real_quine.py ----
import sys

for line in open(sys.argv[0]):
print line,

--------------------------------------
;-)

--
Ant.

0 new messages