If you don't know what a quine is, it is a program or script that's only purpose or function is to print out its own source code.

I was wondering would this be considered a quine?

f = open("quine.py","r"); print f.read()

is the script that this code is in. It does print out itself like a quine would.

Dani AI

Generated

’s snippet reads and prints its own file. By the usual, formal definition that disqualifies it: a true quine must produce its exact source text without reading it from a file, network, or other external input. As noted, file I/O makes the task trivial. ’s “persistant quine” isn’t standard terminology — if meant to imply a program that re-reads or re-writes its file, that still relies on external storage and therefore isn’t a canonical quine.

A common, language-idiomatic technique is to store the program as a quoted template and then print that template with the template’s own representation inserted. A compact Python 3 example that follows this rule:

s = 's = {0!r}\nprint(s.format(s))'
print(s.format(s))

Mechanics: the string s contains the program text with a placeholder. {0!r} is replaced by Python’s repr(s), which includes the quoting and escape sequences needed to reconstruct s verbatim. The result of s.format(s) is the program source itself, printed to stdout.

Practical notes: a quine requires byte-for-byte equality between output and source, so line endings and file encoding matter. The file-read approach can be handy for simple scripting tasks but does not meet the standard quine requirement. Size or “byte-count” claims (such as the 37‑byte remark) are orthogonal: brevity is interesting for code-golf, but it does not change whether a program is a quine.

Recommended Answers

All 4 Replies

A persistant quine perhaps ?

I don't understand what you mean by persistent quine.
The idea struck me a few nights ago before I went to sleep. If it is in fact a quine, its a pretty good one at only 37 bytes.

Awww.... I thought I had one... Oh well lol.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.