How to assign a memory address to a variable in Python?

前端 未结 2 755
暖寄归人
暖寄归人 2020-12-16 06:30

here\'s the scenario:

I foolishly forget to assign the returned object to a variable:

>>> open(\"random_file.txt\")


        
2条回答
  •  萌比男神i
    2020-12-16 06:49

    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
    
    >>> f = _ # Not to worry. It's stored as _
    >>> f
    
    >>> 
    

提交回复
热议问题