简体   繁体   中英

Python - reference object in memory by address

This is kind of a silly question, but I'm just curious about it.

Suppose I'm at the Python shell and I have some database object that I query. I do:

db.query(queryString)

The query returns a response <QueryResult object at 0xffdf842c> or something like that.

But then I say "Oh! I forgot to put result = db.query(queryString) so that I can actually use the result of my query!"

Well isn't there some method that just lets me reference the object in memory?

Something like result = reference('<QueryResult object at 0xffdf842c>') ?

You can do:

>>> result=_

at the shell. _ represents the last calculated object.

Example:

>>> iter(range(10))
<listiterator object at 0x10ebcccd0>
>>> result=_
>>> result
<listiterator object at 0x10ebcccd0>
>>> list(result)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

You can also see the string representation of the object (if the object type supports that) by typing repr(_)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM