Simple Quine in Python

I recently saw this blogpost – http://philipsung.blogspot.com/2010/12/people-who-are-not-in-our-league-v.html – which gives a quine in ruby with a twist (read the post). It struck me that somehow I’ve never tried at writing a quine.

Turns out a very simple quine (no attempt be concise) is.. very simple.

import zlib
data = b'x\x9c\xcb\xcc-\xc8/*Q\xa8\xca\xc9L\xe2JI,IT\xb0U\xa86\xa8\xe5*(\xca\xcc+\xd1\x00\x89\xea\xa5\xa4&\xe7\xe7\x16\x14\xa5\x16\x17k\x80\x14h\x82\x05RR5\xd4KK\xd2t-\xd45\xf5\xd2\xf2\x8br\x13K \x92\x9a\x00\x82\x1e\x1b\xc4'
print(zlib.decompress(data).decode('utf-8').format(data))

The data section is result of compressing the string:

import zlib
data = {0}
print(zlib.decompress(data).decode('utf-8').format(data))

and so we decompress the data and replace {0} with what the data is.

About these ads

One Response to Simple Quine in Python

  1. I’m not entirely sure why you are using zlib, have you considered this version?
    data = ‘data = {0}\nprint(data.format(repr(data)))’
    print(data.format(repr(data)))

    Quines are kinda tricky, so it’s nice that there is some theory behind them (ie. some deeper truth)
    http://en.wikipedia.org/wiki/Kleene%27s_recursion_theorem
    (I have no idea how the theorem works, but you might be able to figure it out)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s