[IronPython] __repr__ and __str__ for .NET types
nbastin at opnet.com
Tue May 23 07:30:19 PDT 2006
On May 23, 2006, at 8:57 AM, Jonathan Jacobs wrote:
> In all the cases I've seen, calling repr on a .NET type ends up
> ToString which is generally reasonably useful but almost always
> 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()
> 'X: 0\nY: 0\nZ: 0\n'
> and something more like:
>>>> class Foo(object): pass
> '<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
More information about the users