logo

Kvins Python valodā

Quine ir programma, kas neizmanto ievadi, bet izvada sava koda kopiju. Mēs esam apsprieduši kinīns 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 (_%_) 
Paskaidrojums: Iepriekš minētais kods ir klasisks virknes formatējuma lietojums. Pirmkārt, mēs definējam mainīgo _ un piešķirot tai '_=%r;print _%%_'. Otrkārt, mēs drukājam _%_ . Šeit mēs drukājam _ ar _ kā ievadi virknes formatēšanai. Tātad %r iekšā _ iegūst _ vērtību. Jūs pat varat izmantot %s vietā %r . Mēs izmantojām dubultā % '_=%r;drukājiet _%%_', lai aizbēgtu % . 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 atvērt () funkcija, lai izdrukātu tā avota kodu.