[IronPython] __repr__ and __str__ for .NET types

Nicholas Bastin nbastin at opnet.com
Tue May 23 16:30:19 CEST 2006


On May 23, 2006, at 8:57 AM, Jonathan Jacobs wrote:

> Hi,
>
> In all the cases I've seen, calling repr on a .NET type ends up  
> calling
> ToString which is generally reasonably useful but almost always  
> unnecessarily
> verbose. Sometimes it would be nice if it gave you something a  
> little more
> specific about the object in question, something less like:
>
>>>> v = Microsoft.DirectX.Vector3()
>>>> repr(v)
>   'X: 0\nY: 0\nZ: 0\n'
>
> and something more like:
>
>>>> class Foo(object): pass
>>>> ...
>>>> repr(Foo())
>   '<Foo object at 0x000000000000002B>'
>
> and leaving __str__ to return the ToString value.
>
> This behaviour also fits the CPython documentation
> (http://docs.python.org/ref/customization.html), for these two  
> functions, more
> closely than the current behaviour, IMO.

No, actually it doesn't.  The string returned by __repr__ should be  
able to be used to re-create the object, if that is possible.  In  
this case,  'X: 0\nY: 0\nZ: 0\n' is a *lot* closer to re-creating the  
object than  '<Foo object at 0x000000000000002B>' is.  The second  
case is what gets returned when it isn't possible to re-create the  
object, but that obviously isn't the case in the example of  
DirectX.Vector3.


--
Nick



More information about the Ironpython-users mailing list