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

Steven D'Aprano steve at pearwood.info
Sun Oct 23 06:26:16 CEST 2011


Antoine Pitrou wrote:
[...]
> It might be the integer 1 or the string "1". You need repr() to tell
> the difference.

Antoine, that's a cheap shot. The *very next paragraph* of my post which 
you replied to, and which you snipped out of your response, says:

     I am aware that the string representation (using either __str__
     or __repr__) of an object is not the definitive word in what
     the object really is. Using just print, one can't distinguish
     between a class and a string "<class '__main__.Spam'>", or for
     that matter between the string 2 and the int 2, and that
     arbitrary objects can return arbitrary strings. Nevertheless,
     I like the __str__ of classes, modules and functions just the
     way they are.


Can we please stop throwing up this red herring? Uniqueness of the str() 
or repr() of an object is not and has never been a requirement, which is 
fortunate since it is physically impossible.



-- 
Steven




More information about the Python-ideas mailing list