How To Assign A Memory Address To A Variable In Python?
here's the scenario: I foolishly forget to assign the returned object to a variable: >>> open('random_file.txt')
Solution 1:
Python is a high level language. You can't (not directly anyway) mess with memory addresses so the answer is no.
The REPL however does conveniently store the result of the last expression in a magic variable _
. You can fetch it from there. To quote your example.
>>> open("/etc/passwd","r") #Oops I forgot to assign it
<open file '/etc/passwd', mode 'r' at 0x7f12c58fbdb0>
>>> f = _ # Not to worry. It's stored as _>>> f
<open file '/etc/passwd', mode 'r' at 0x7f12c58fbdb0>
>>>
Post a Comment for "How To Assign A Memory Address To A Variable In Python?"