What does this Python program do?

  • Context: Python 
  • Thread starter Thread starter Demystifier
  • Start date Start date
  • Tags Tags
    Program Python
Click For Summary

Discussion Overview

The discussion revolves around understanding a specific Python program that prints its own source code. Participants explore the concept of quines, the mechanics of the program, and comparisons with similar constructs in other programming languages.

Discussion Character

  • Exploratory
  • Technical explanation
  • Conceptual clarification
  • Historical

Main Points Raised

  • One participant states that the program prints itself, explaining the use of the %r or repr() function in Python, which returns a raw string that can recreate the input value.
  • Another participant shares a nostalgic anecdote about a BASIC program that simulated a command prompt, drawing parallels to the Python program's self-replicating nature.
  • Some participants note that creating a program that prints itself is possible in any programming language, highlighting Python's simplicity in achieving this compared to others.
  • A comparison is made to a Lisp example that functions similarly to the Python quine, showcasing that other languages can also implement this concept effectively.

Areas of Agreement / Disagreement

Participants generally agree on the nature of the Python program as a quine and its simplicity compared to other languages. However, there are varying perspectives on the historical context and examples of similar programs in different languages.

Contextual Notes

Some discussions reference external examples and concepts without resolving the nuances of how different programming languages handle self-replicating code.

Who May Find This Useful

Readers interested in programming concepts, particularly quines, and those exploring the similarities and differences in programming languages may find this discussion beneficial.

Demystifier
Science Advisor
Insights Author
Messages
14,718
Reaction score
7,311
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   Reactions: 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   Reactions: QuantumQuest and Demystifier

Similar threads

  • · Replies 11 ·
Replies
11
Views
2K
Replies
1
Views
2K
Replies
6
Views
3K
  • · Replies 10 ·
Replies
10
Views
4K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
5
Views
2K
  • · Replies 9 ·
Replies
9
Views
5K
  • · Replies 2 ·
Replies
2
Views
1K