This is a bit of a random question, which is more out of curiosity than any particular need.
Is it possible to write some python code that will print some things, including the source code itself, without the python code stored in the file? For example, by doing something similar in the Bash prompt:
$ echo ' > print "The Code:" > PrintScript() # What would this function look like? > for i in range(5): > print i, > print "!" > ' | python
and get this conclusion:
The Code: print "The Code:" PrintScript() # What would this function look like? for i in range(5): print i, print "!" 0 1 2 3 4 5 !
I suspect that this probably cannot be done, but given the possibilities of introspecting python, I was curious to see if this level extends to this level.
What is the closest I get:
echo 'import __main__,inspect;print inspect.getsource(__main__)' | python
... , ( stdin) . __main__.
__main__
Update:
dis , , :
dis
$ echo -e 'import __main__,dis;print dis.dis(__main__)' | python None
:
$ echo -e "import __main__,dis;print dis.dis(__main__)\ndef x():\n pass" | python None
, , . , ( ).
Quine, Python:
quine = 'quine = %r\r\nprint quine %% quine' print quine % quine
quines . , , ...:)
print open(__file__).read(),
UNIX, , Windows. , , . a >
( ), , sys.stdin, . , quines (, ) Python, , . , , .
sys.stdin
, , , , , . , Python , . (, shebang, , ).
readline , , , , , ,
Source: https://habr.com/ru/post/1752420/More articles:Riskless stretch ScrollledWindow - pythonWant to download a client-side GWT application .cache.html file - htmluse custom template file for main page and product page magento - xmlhttps://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1752418/many-roundtrips-to-the-database-or-all-data-filter&usg=ALkJrhjGahGYO5AgRXqmh4tcRNRZVGaZzgBindingList for DTO that maintains the state of remote objects - c #Manually activate user account in asp.net - asp.net-membershipCross-site scripting prevention in PHP - securityПодключение к разъему Bluetooth - androidIs the Intelligent Transfer Service (BITS) data rate very slow? - c #Changing labels in JFace TreeViewer on node extension / rollup - swtAll Articles