When python gives me the location of an object in memory, what is that for, other than distinguishing between instances in the interactive prompt?
Example:
>>>inspect.walktree
<function walktree at 0x2a97410>
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
This is the default string representation that is returned if you call
repr(obj)on an object which doesn’t define the magic__repr__method (or didn’t override the default implementation inherited fromobject, in the case of new-style objects).That default string has the purpose of giving the programmer useful information about the type and identity of the underlying object.
Additional information
Internally, the
idfunction is called to get the number included in the string:Note that
iddoes NOT represent a unique ID. It can happen that during the lifetime of a program several objects will have the same ID (although never at the the same time).It also does not have to correlate with the location of the object in memory (although it does in CPython).
You can easily override the representation string for your own classes, by the way: