[Python-ideas] Reprs of classes and functions

Georg Brandl g.brandl at gmx.net
Thu Feb 26 06:54:57 CET 2015


On 02/25/2015 09:12 AM, Serhiy Storchaka wrote:
> This idea is already casually mentioned, but sank deep into the threads 
> of the discussion. Raise it up.
> 
> Currently reprs of classes and functions look as:
> 
>  >>> int
> <class 'int'>
>  >>> int.from_bytes
> <built-in method from_bytes of type object at 0x826cf60>
>  >>> open
> <built-in function open>
>  >>> import collections
>  >>> collections.Counter
> <class 'collections.Counter'>
>  >>> collections.Counter.fromkeys
> <bound method Counter.fromkeys of <class 'collections.Counter'>>
>  >>> collections.namedtuple
> <function namedtuple at 0xb6fc4adc>
> 
> What if change default reprs of classes and functions to just full 
> qualified name __module__ + '.' + __qualname__ (or just __qualname__ if 
> __module__ is builtins)? This will look more neatly.

Like other, I would be -1 because of the increased potential for confusion
(many people have not grasped the significance of repr() and its quoting
of strings), and the loss of information (class/function/...).

It would be nice to have the repr()s consistent, i.e. name always or never
in quotes, and always or never add the memory address.  But that's probably
not worth breaking compatibility.

Changing str(class) is different, since there's enough potential confusion
in this representation anyway.

> And such reprs are evaluable.

Only sometimes, unless you change the repr to
"__import__(" + __module__ + ", None, None, ['*'])." + __qualname__

:)

Georg



More information about the Python-ideas mailing list