On Tue, Apr 28, 2009 at 3:39 PM, Marius Gedminas <mar...@gedmin.as> wrote > > > >>> 1.8 misc > >>> ====================== > >>> 1.8.1 support iter protocol > >>> 1.8.2 str(v) -> "[x, y, z]" > >>> 1.8.3 repr(v) -> "Vec<x, y, z>" > >> > >> Most objects have the same output for str() and repr(). Why do you > >> differ here? > > > > with repr I wanted to make explicitly clear that this is not a list but I > > thought that the normal str() would look nicer that way. what would you > > suggest? "[x, y, z]" for both (or "(x, y, z)" if we choose to make > > vectors immutable) or "Vector3d<x, y, z>"? > > I'm +0.95 for distinct __str__ and __repr__. str is what you show your > users and should be short and clear, while repr is what you give the > programmers who're debugging and therefore exact types matter. >
As far as I understand it, __repr__ is supposed to give you a representation that can reproduce an equivalent object. In other words, eval(repr(ob)) == ob. I'm not sure if this property is true in all cases where __repr__ has been used, but it holds for python standard types. I think it's more of a consideration than a full on standard. Still, I would prefer then to have the __repr__ be "Vector3d(x, y, z)" if that is in fact the constructor of a vector3d. Of course this doesn't matter much.