{"id":115853,"date":"2020-07-26T06:22:46","date_gmt":"2020-07-26T06:22:46","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=11385"},"modified":"2020-07-26T06:22:46","modified_gmt":"2020-07-26T06:22:46","slug":"python-one-line-quine","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2020\/07\/26\/python-one-line-quine\/","title":{"rendered":"Python One Line Quine"},"content":{"rendered":"<p>Most computer scientists, programmers, and hackers don&#8217;t even know the meaning of the word &#8220;Quine&#8221; in the context of programming. So, first things first:<\/p>\n<h2>What is a Quine?<\/h2>\n<p><strong>Roughly speaking, a quine is a self-reproducing program: if you run it, it generates itself.<\/strong><\/p>\n<p>Here&#8217;s a great definition:<\/p>\n<p><em>:<strong>quine<\/strong>: \/kwi:n\/ \/n.\/ [from the name of the logician Willard van Orman Quine, via Douglas Hofstadter] A program that generates a copy of its own source text as its complete output. Devising the shortest possible quine in some given programming language is a common hackish amusement. (<a href=\"http:\/\/www.nyx.net\/~gthompso\/quine.htm\" target=\"_blank\" rel=\"noreferrer noopener\" title=\"http:\/\/www.nyx.net\/~gthompso\/quine.htm\">source<\/a>)<\/em><\/p>\n<blockquote class=\"wp-block-quote\">\n<p>The name &#8220;quine&#8221; was coined by Douglas Hofstadter, in his popular science book <em>G\u00f6del, Escher, Bach<\/em>, in honor of philosopher Willard Van Orman Quine (1908\u20132000), who made an extensive study of indirect self-reference, and in particular for the following paradox-producing expression, known as Quine&#8217;s paradox.<\/p>\n<p><cite><a href=\"https:\/\/en.wikipedia.org\/wiki\/Quine_(computing)\" target=\"_blank\" rel=\"noreferrer noopener\" title=\"https:\/\/en.wikipedia.org\/wiki\/Quine_(computing)\">Wikipedia<\/a><\/cite><\/p><\/blockquote>\n<p>The shortest possible quine is the following empty program:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"> <\/pre>\n<p>The program is self-reproducing because the output of the program is the program itself. Go ahead and run it in your own shell! <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/72x72\/1f609.png\" alt=\"\ud83d\ude09\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><\/p>\n<p>Let&#8217;s dive into a collection of Python Quines to demonstrate how they work!<\/p>\n<h2>Python One-Liner Quine 1<\/h2>\n<p>Here&#8217;s a short one-liner Quine, I found at <a href=\"https:\/\/cs.lmu.edu\/~ray\/notes\/quineprograms\/\" target=\"_blank\" rel=\"noreferrer noopener\" title=\"https:\/\/cs.lmu.edu\/~ray\/notes\/quineprograms\/\">this <\/a>resource:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">s='s=%r;print(s%%s,sep=\"\")';print(s%s,sep=\"\")<\/pre>\n<ul>\n<li>You may want to improve your <a href=\"https:\/\/pyformat.info\/\" target=\"_blank\" rel=\"noreferrer noopener\" title=\"https:\/\/pyformat.info\/\">string formatting skills<\/a> to really understand this one-liner. <\/li>\n<li>The <a href=\"https:\/\/blog.finxter.com\/the-separator-and-end-arguments-of-the-python-print-function\/\" title=\"Python Print Function [And Its SECRET Separator &amp; End Arguments]\" target=\"_blank\" rel=\"noreferrer noopener\">separator argument of the print function<\/a> avoids printing a new-line character at the end. <\/li>\n<li>The <code>%r<\/code> format specifier automatically supplies single quotes.<\/li>\n<\/ul>\n<p>Here&#8217;s the code in an interactive shell so that you can play with this Quine in your browser:<\/p>\n<p> <iframe loading=\"lazy\" height=\"400px\" width=\"100%\" src=\"https:\/\/repl.it\/@finxter\/SnoopyEnviousComments?lite=true\" scrolling=\"no\" frameborder=\"no\" allowtransparency=\"true\" allowfullscreen=\"true\" sandbox=\"allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts allow-modals\"><\/iframe> <\/p>\n<p><em><strong>Exercise<\/strong>: Run the code. What&#8217;s the output? Can you explain why?<\/em><\/p>\n<h2>Python One-Liner Quine 2<\/h2>\n<\/p>\n<p> <iframe loading=\"lazy\" height=\"400px\" width=\"100%\" src=\"https:\/\/repl.it\/@finxter\/ForkedCloudyDestination?lite=true\" scrolling=\"no\" frameborder=\"no\" allowtransparency=\"true\" allowfullscreen=\"true\" sandbox=\"allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts allow-modals\"><\/iframe> <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Most computer scientists, programmers, and hackers don&#8217;t even know the meaning of the word &#8220;Quine&#8221; in the context of programming. So, first things first: What is a Quine? Roughly speaking, a quine is a self-reproducing program: if you run it, it generates itself. Here&#8217;s a great definition: :quine: \/kwi:n\/ \/n.\/ [from the name of the [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[857],"tags":[73,468,528],"class_list":["post-115853","post","type-post","status-publish","format-standard","hentry","category-python-tut","tag-programming","tag-python","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/115853","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=115853"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/115853\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=115853"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=115853"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=115853"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}