You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Yes. So if we are not going to change other built-in types, maybe we'd better not change bytearray either. My opinion is that don't change built-in classes, even bytearray. If users would like a more reasonable repr, they can provide a custom __repr__ as your example. But make such changes to classes in collections sounds like a good idea to me.
It certainly seems that collections should be consistent about this. The question of builtin types is a different issue, and I agree that it is probably way more trouble than it is worth to change them, especially since, for example, repr(str) is often used just to get the quote marks in contexts where you *don't* want the subclass name.
It looks to me that the repr of a collection contains a dynamic name if it is implemented in Python and hardcoded base name if it is implemented in C (OrderedDict originally was implemented in Python). Maybe just because tp_name contains full qualified name, and extracting a bare class name needs few lines of code.
There is similar issue with the io module classes: bpo-21861.
Since this problem is already solved for OrderedDict, I think it is easy to use this solution in other classes. Maybe factoring out the following code into helper function.
constchar*classname;
classname=strrchr(Py_TYPE(self)->tp_name, '.');
if (classname==NULL)
classname=Py_TYPE(self)->tp_name;
elseclassname++;
+1 for changing the cases Serhiy found. Also, I agree with Serhiy that there should not be a change for the built-in types that have a literal notation (it has been this was forever, hasn't caused any real issues, and changing it now will likely break code).
New changeset b3a7796 by Serhiy Storchaka in branch 'master': bpo-27541: Reprs of subclasses of some classes now contain actual type name. (bpo-3631) b3a7796
Because reprs of Python implementations of collection use a bare __name__.
__qualname__ is used only in combination with __module__. Using a single __qualname__ can be confused: foo.bar looks as a name bar in the module foo. Whether in reprs and error messages either full qualified name is used ("{cls.__module__}.{qualname}") or a bare __name__. If a displayed name contains a dot it is always a full qualified name.
Activity
serhiy-storchaka commentedon Jul 17, 2016
The repr of subclasses of some collection classes contains a name of the subclass:
But the repr of subclasses of some collection classes contains a name of the base class:
Shouldn't a name of the subclass always be used?
zhangyangyu commentedon Jul 17, 2016
How about other built-in classes? If repr does matter, maybe str, int, dict should also respect this rule?
serhiy-storchaka commentedon Jul 17, 2016
This can break third-party code. For example the code that explicitly makes the repr containing a subclass name:
I think the chance of breaking third-party code for bytearray or deque is smaller, since the repr is not literal.
zhangyangyu commentedon Jul 17, 2016
Yes. So if we are not going to change other built-in types, maybe we'd better not change bytearray either. My opinion is that don't change built-in classes, even bytearray. If users would like a more reasonable repr, they can provide a custom __repr__ as your example. But make such changes to classes in collections sounds like a good idea to me.
bitdancer commentedon Jul 17, 2016
It certainly seems that collections should be consistent about this. The question of builtin types is a different issue, and I agree that it is probably way more trouble than it is worth to change them, especially since, for example, repr(str) is often used just to get the quote marks in contexts where you *don't* want the subclass name.
serhiy-storchaka commentedon Jul 17, 2016
It looks to me that the repr of a collection contains a dynamic name if it is implemented in Python and hardcoded base name if it is implemented in C (OrderedDict originally was implemented in Python). Maybe just because tp_name contains full qualified name, and extracting a bare class name needs few lines of code.
There is similar issue with the io module classes: bpo-21861.
Since this problem is already solved for OrderedDict, I think it is easy to use this solution in other classes. Maybe factoring out the following code into helper function.
rhettinger commentedon Sep 17, 2017
+1 for changing the cases Serhiy found. Also, I agree with Serhiy that there should not be a change for the built-in types that have a literal notation (it has been this was forever, hasn't caused any real issues, and changing it now will likely break code).
serhiy-storchaka commentedon Sep 21, 2017
New changeset b3a7796 by Serhiy Storchaka in branch 'master':
bpo-27541: Reprs of subclasses of some classes now contain actual type name. (bpo-3631)
b3a7796
serhiy-storchaka commentedon Sep 21, 2017
Thanks Raymond.
vstinner commentedon Sep 21, 2017
Why using type.__name__ rather than type.__qualname__?
serhiy-storchaka commentedon Sep 21, 2017
Because reprs of Python implementations of collection use a bare __name__.
__qualname__ is used only in combination with __module__. Using a single __qualname__ can be confused: foo.bar looks as a name bar in the module foo. Whether in reprs and error messages either full qualified name is used ("{cls.__module__}.{qualname}") or a bare __name__. If a displayed name contains a dot it is always a full qualified name.
vstinner commentedon Sep 21, 2017
Ah, maybe this module should be updated to use qualified name with the name in repr()?
I was thinking at module.qualname, right.