classification
Title: Hexadecimal id in reprs
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.5
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: serhiy.storchaka Nosy List: haypo, python-dev, serhiy.storchaka, steven.daprano
Priority: normal Keywords: patch

Created on 2014-07-22 08:50 by serhiy.storchaka, last changed 2014-07-22 11:52 by haypo. This issue is now closed.

Files
File name Uploaded Description Edit
repr_hex_id.diff serhiy.storchaka, 2014-07-22 08:50 review
Messages (6)
msg223647 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2014-07-22 08:50
Default repr of an instance looks as:

>>> object()
<object object at 0xb71a14a8>

Many specialized reprs follow a pattern <... at {hexadecimal id}...>. But there are few deviations:

1. Reprs of WeakValueDictionary and WeakKeyDictionary in the weakref module output decimal id.

2. reprlib and reprs of CDLL in the ctypes module and DateTime and MultiCall in the xmlrpc.client module output hexadecimal id but without the "0x" prefix.

Proposed patch makes these cases to conform with other reprs.
msg223648 - (view) Author: STINNER Victor (haypo) * (Python committer) Date: 2014-07-22 08:53
The patch looks good to me.
msg223652 - (view) Author: Roundup Robot (python-dev) Date: 2014-07-22 09:14
New changeset 4cef7b0ec659 by Serhiy Storchaka in branch 'default':
Issue #22031: Reprs now always use hexadecimal format with the "0x" prefix
http://hg.python.org/cpython/rev/4cef7b0ec659
msg223653 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2014-07-22 09:17
Thanks Victor. Applied only in default because this issue is too minor.
msg223660 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2014-07-22 11:48
> Many specialized reprs follow a pattern <... at {hexadecimal id}...>. 
> But there are few deviations:
[...]
> Proposed patch makes these cases to conform with other reprs.

I oppose making this change. The exact format of this generic repr of 
instances is not a part of Python's API, and we should not encourage 
people to think that it must always follow the same pattern or rely on 
that pattern.

Changing the repr risks breaking doctests. That *might* be acceptable if 
there was some obvious benefit to the change, e.g. changing from some 
generic <whatever at some_id> to a nicer, more useful display. But to 
risk breaking doctests just for consistency with other generic displays 
when that consistency has never been promised is not worth it.
msg223662 - (view) Author: STINNER Victor (haypo) * (Python committer) Date: 2014-07-22 11:52
> Changing the repr risks breaking doctests.

That's why the change was only done in Python 3.5. To port your application to Python 3.5, you can modify you doctests to use "..." (ellipsis option of doctests).

> That *might* be acceptable if there was some obvious benefit
> to the change,

It's better to have consistent representation of objects. Low-level debuggers like gdb uses hexadecimal, not decimal. I'm regulary using gdb, and having the memory address in hexadecimal helps.

By the way, you should never rely on the (exact) representation in your unit tests :-)
History
Date User Action Args
2014-07-22 11:52:11hayposetmessages: + msg223662
2014-07-22 11:48:07steven.dapranosetnosy: + steven.daprano
messages: + msg223660
2014-07-22 09:17:52serhiy.storchakasetstatus: open -> closed
messages: + msg223653

assignee: serhiy.storchaka
resolution: fixed
stage: patch review -> resolved
2014-07-22 09:14:57python-devsetnosy: + python-dev
messages: + msg223652
2014-07-22 08:53:05hayposetnosy: + haypo
messages: + msg223648
2014-07-22 08:50:42serhiy.storchakacreate