here's scenario:
i foolishly forget assign returned object variable:
>>> open("random_file.txt") <open file 'random_file.txt', mode 'r' @ 0x158f780>
is there way directly assign memory address variable? equivalent to
int *ptr; *ptr = 0x158f780;
counter-points:
in case can discard object - file's opened in read mode , object collected gc. i'm interested in solution scenario need free resource in deterministic way.
i assume "at 0x158f780" part id() of object - happens memory address in cpython ("this implementation-specific , may change, blah-blah"). i'm interested in both scenarios - binding variable id() value great (more portable , not), binding bare memory address o.k.
i did google around bit "python pointer"-related, variable/memory address assignment , similar, seems google-fu not strong enough.
cheers,
edit:
some tangents after reading answers , browsing python docs: file object referenced repl _ var. _ assigned result of next command, assume refcount goes 0. file object removed memory, , if assume implements sane __del__
method, should take care of low-level details, i.e. closing file , on. http://docs.python.org/reference/datamodel.html#object.__del__ details general scenario.
that , i'll adding 0.0.0.0 stackoverflow.com /etc/hosts...
python high level language. can't (not directly anyway) mess memory addresses answer no.
the repl conveniently store result of last expression in magic variable _
. can fetch there. quote example.
>>> open("/etc/passwd","r") #oops forgot assign <open file '/etc/passwd', mode 'r' @ 0x7f12c58fbdb0> >>> f = _ # not worry. it's stored _ >>> f <open file '/etc/passwd', mode 'r' @ 0x7f12c58fbdb0> >>>
Comments
Post a Comment