[Python-ideas] Changing str(someclass) to return only the class name

Guido van Rossum guido at python.org
Wed Nov 2 18:26:20 CET 2011


-1. I'd like it so that (given a suitable set of imports) if you typed
back to the interpreter what it printed at you, you get the same thing
back again.

>>> print(x)
42
>>> print(42)
42
>>> print(y)
None
>>> print(None)
None
>>> print(z)
foo
>>> print(foo)
foo

Ping's proposal would goes against this:

>>> print(x)
foo()
>>> print(foo())
42
>>> print(Splat)
class Splat
>>> print(class Splat)
SyntaxError: invalid syntax

I'm expecting that in most cases there is enough redundancy in the
name that you'll know what kind of thing it is. And if you want to
know for sure, continue to use repr() -- or, at the interactive
prompt, just omit the print() call, since the interactive interpreter
automatically calls repr() on your expression.

--Guido

On Fri, Oct 28, 2011 at 3:00 PM, Ka-Ping Yee <python at zesty.ca> wrote:
> Hi there,
>
> I get that repr() is supposed to be the precise representation
> and str() is intended more to be friendly than precise.  My
> concern with the proposal is just that this:
>
>    >>> print x
>    foo
>
> ...doesn't actually feel that friendly to me.  I want to know
> that it's *probably* a function or *probably* a class, the same
> way that today, when I see:
>
>    >>> print x
>    biscuit
>
>    >>> print y
>    [1, 2, 3]
>
> I can guess that x is *probably* a string and y is *probably*
> a list (e.g. because I know I'm not working with any custom
> objects whose __str__ returns those things).
>
> It would create a slightly higher mental burden (or slightly
> higher probability of human error) if, when I see:
>
>    >>> print x
>    Splat
>
> ...I have to remember that x might be a string or a function or
> a class.
>
> I'd just like some kind of visual hint as to what it is.  Like:
>
>    >>> print x
>    foo()
>
> or:
>
>    >>> print x
>    function foo
>
> or:
>
>    >>> print x
>    function foo(a, b)
>
> or:
>
>    >>> print x
>    class Bar
>
> In fact "function foo(a, b)" would actually be rather useful
> in a lot of situations, and I would argue, friendlier than "foo".
>
>
> --Ping
>



-- 
--Guido van Rossum (python.org/~guido)



More information about the Python-ideas mailing list