3

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>')?

0

1 Answer 1

7

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(_)

Sign up to request clarification or add additional context in comments.

4 Comments

oh, _ is the most recently returned object?
@John Peter Thompson Garcés: Yes
So, what if I want the result of some previous expression? I just read that in IPython you can do _2, _3, etc. But is there anything that can do this in the standard Python shell?
@John Peter Thompson Garcés: I am pretty sure the default Python shell only stores a single object output (unlike iPython...)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.