logo

Quine Pythonissa

Quine on ohjelma, joka ei syötä syötettä vaan tulostaa kopion omasta koodistaan. Olemme keskustelleet quine C:ssä . 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 (_%_) 
Selitys: Yllä oleva koodi on klassinen merkkijonomuotoilun käyttö. Ensin määritämme muuttujan _ ja määrittämällä sille '_=%r;print _%%_'. Toiseksi tulostamme _%_ . Tässä tulostetaan _ ja _ syötteenä merkkijonomuotoilulle. Niin %r in _ saa arvon _. Voit jopa käyttää %s sijasta %r . Käytimme tuplaa % '_=%r;tulosta _%%_' poistuaksesi % . 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 avata() toiminto tulostaa sen lähdekoodin.