[Python-ideas] Reprs of classes and functions

Serhiy Storchaka storchaka at gmail.com
Wed Feb 25 12:08:21 CET 2015


On 25.02.15 12:25, Steven D'Aprano wrote:
> On Wed, Feb 25, 2015 at 10:12:03AM +0200, Serhiy Storchaka wrote:
>> 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. And such reprs are
>> evaluable.
>
> Do you mean like this?
>
> repr(int)
> => 'int'
>
> repr(int.from_bytes)
> => 'int.from_bytes'

Yes, it is.

> -1 on that idea.
>
> The suggested repr gives no clue as to what kind of object they are. Are
> they functions, methods, classes, some kind of Enum-like constant or
> something special like None? That hurts the usefulness of object reprs
> at the interactive interpreter. And it leads to weirdness like this:
>
>
> def spam(x):
>      if not isinstance(x, int):
>          raise TypeError('expected int, got %r' % x)

This is uncommon case and bugprone code. Don't write so. 
spam('x'*1000000) or spam(()) will produce unhelpful error messages.

Usually it is written as:

def spam(x):
      if not isinstance(x, int):
          raise TypeError('expected int, got %s' % type(x).__name__)




More information about the Python-ideas mailing list