Ty's Creatively Named Blog

Monday, February 27, 2006

A Python Quine (at least there is symmetry)

Several years ago now, I think, I ran into a webpage with a collection of Quines in different languages. The Python entry was rather disappointing -- too long and unoriginal. I've seen a number of better ones since, but at the time I was inspired to create one that was shorter and used a unique feature of the language. So without further ado:

'print `__doc__`;print __doc__'
print `__doc__`;print __doc__

It can be shortened 5 bytes by using a temporary variable:

'd=__doc__;print`d`;print d'
d=__doc__;print`d`;print d


However, this version is still almost twice as long as the shortest Python quine I know of. So I will stick with the original. It may not be the shortest, but at least there is symmetry.

0 Comments:

Post a Comment

<< Home