Python What does this Python program do?

  • Thread starter Thread starter Demystifier
  • Start date Start date
  • Tags Tags
    Program Python
AI Thread Summary
The Python program in question is a quine, which is a self-replicating program that outputs its own source code. The key component is the use of the %r or repr() function, which returns a raw string representation that can recreate the original value. The program substitutes the string variable 's' into itself, allowing it to print its own code in proper Python syntax. This concept is not unique to Python; similar self-replicating programs exist in other programming languages, but Python's implementation is notably simple. The discussion also references historical examples of similar programs, such as a BASIC program that simulated a command prompt and produced fake outputs, illustrating the long-standing interest in self-replicating code.
Demystifier
Science Advisor
Insights Author
Messages
14,564
Reaction score
7,158
Can someone tell me what does this Python program do? :wink:
Python:
s = 's = %r\nprint(s%%s)'
print(s%s)

Mentor: add html code tags
 
Last edited by a moderator:
Technology news on Phys.org
Well its printing itself.

Here's an explanation of the %r or repr() function which returns a raw string in python syntax that can be used to recreate the input value.

https://stackoverflow.com/questions/6005159/when-to-use-r-instead-of-s-in-python

so start with the print statement its going to substitute the string s into the string s at the point of the %s in the string s.

print(s%'a') would produce
Python:
s = 'a'
print(s%s)

The end result is a program that prints itself in proper python syntax so you can run it again. Its kind of a recursive loop except the looping is missing.
 
Last edited:
  • Like
Likes QuantumQuest and Demystifier
This reminds me of the early 1980's story of a kid in school who wrote a program in BASIC that simulated the command prompt and printed fake answers. It responded to 'list' to print a fake program and to 'run' to give a fake answer.

Code:
$ list
10 print 1+1
$ run
3
$ 10 print 1+2
$ run
5
$ list
10 print 1+2
...

The teacher was completely baffled.

I discovered another one online that was truly epic with a fake DOS shell:

http://stevehanov.ca/blog/index.php?id=79
 
Demystifier said:
A program which prints itself is possible in any program language, but it seems that in no other language such a program looks so simple: https://en.wikipedia.org/wiki/Quine_(computing)

Well it works because Python supports printing objects in their literal (source) form. Python isn't the first language to support this. This Lisp example works exactly the same way as the Python version:
Code:
(let ((s "(let ((s ~s))~%  (format t s s))~%"))
  (format t s s))
One of the examples on Rosetta Code uses a reader macro to slightly shorten this (slightly modified here):
Code:
(format t #1="(format t #1=~s #1#)~%" #1#)
 
  • Like
Likes QuantumQuest and Demystifier
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
10
Views
3K
Replies
5
Views
1K
Replies
10
Views
1K
Replies
3
Views
2K
Replies
2
Views
1K
Replies
4
Views
3K
Back
Top