[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Python self-evaluating strings

Arnaud Delobelle

1/27/2008 4:14:00 PM

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

4 Answers

dg.google.groups

1/27/2008 6:47:00 PM

0

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

1/27/2008 7:00:00 PM

0

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
> create a list of such expressions.
>
> Quining's-better-than-ironing'ly yours
>
> --Arnaud
>
> [1] http://www.nyx.net/~gthompso/sel...
>

Arnaud Delobelle

1/28/2008 12:08:00 AM

0

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

Ant

1/29/2008 10:00:00 AM

0

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.