logo

Quine v Pythonu

Quine je program, který nebere žádný vstup, ale vydává kopii svého vlastního kódu. diskutovali jsme quine v C . The shortest possible quine in python is just a single line of code! Python
_='_=%r;print _%%_';print _%_ 
In case of Python3.x Python
_='_=%r;print (_%%_)';print (_%_) 
Vysvětlení: Výše uvedený kód je klasické použití formátování řetězců. Nejprve definujeme proměnnou _ a jeho přiřazení '_=%r;print _%%_'. Za druhé tiskneme _%_ . Zde tiskneme _ s _ jako vstup pro formátování řetězce. Tak %r v _ získá hodnotu _. Můžete dokonce použít %s místo %r . Použili jsme dvojité % v '_=%r;vytiskněte _%%_' pro únik % . But you may say that the below code is the smallest right! Python
print open(__file__).read() 
You need to note that it is indeed the smallest python program that can print its own source code but it is not a quine because a quine should not use OTEVŘENO() funkce pro tisk jeho zdrojového kódu.