开发者

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")
<open file 'random_file.txt', mode 'r' at 0x158f780>

Is there a way to directly assign the memory address to a variable? Something roughly equivalent to

int *ptr;
*ptr = 0x158f780;

Counter-points:

  1. In this case I can just discard the object - the file's opened in read mode and the object will be collected by the GC. But I'm interested in a solution for the scenario where I need to free the resource in a deterministic way.

  2. I assume that the "at 0x158f780" part is just the id() of the object - which happens to be the memory address in CPython ("this is implementation-specific and may change, blah开发者_StackOverflow社区-blah"). So I'm interested in both scenarios - binding a variable to the id() value would be great (more portable and what not), but binding it to a bare memory address would also be O.K.

  3. I did google around a bit for anything "python pointer"-related, variable/memory address assignment and similar, but it seems my google-fu is not strong enough.

Cheers,

EDIT:

Some tangents after reading the answers and browsing the python docs: the file object is referenced by the REPL _ var. As soon as _ is assigned to the result of the next command, I assume its refcount goes to 0. The file object is removed from memory, and if we assume it implements a sane __del__ method, it should take care of all the low-level details, i.e. closing the file and so on. http://docs.python.org/reference/datamodel.html#object.__del__ details the general scenario.

That and I'll be adding 0.0.0.0 stackoverflow.com to /etc/hosts...


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>
>>> 


You can't, for the same reason you can't allocate and free memory yourself and can't cast (as in "let's reinterpret this chunk of memory as if it looked like this") things: Dealing with memory on this level is deemed to error-prone, unimportant and automatable to be included in the language. Be glad it's that way, you propably would have eperienced a few crashes and subtle bugs by now caused by fools who though they'd be clever and abuse something along these lines.

As others have states, the last object printed is stored in _ by the REPL, so you can use that if it happens during an interactive session and you catch it soon enough.

(Note that strictly speaking, you could write a CPython extension that provides a function to take a Python int, take raw unboxed value and cast it to a PyObject *. Both awful and impractical.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜