Python self-evaluating strings

A

Arnaud Delobelle

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
 
D

dg.google.groups

It's a bit cheap, but how about

or similarly

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

Dan
 
B

Boris Borcic

Now there's always that style :

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

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 said:
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
 
A

Arnaud Delobelle

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

Ant

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,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,836
Latest member
login dogas

Latest Threads

Top