[IronPython] What C# methods do __repr__ and __str__ use?
Douglas S. Blank
dblank at brynmawr.edu
Wed May 28 22:47:21 CEST 2008
Dino Viehland wrote:
> You'll want to implement the ICodeFormattable interface. It looks
> like we don't pick up a method that's just named __repr__ right now.
> I could see us fixing that but I wouldn't think it's a high priority
> unless someone finds implementing ICodeFormattable burdensome.
Thanks, that did the trick (sample attached below)! It isn't burdensome,
but I wonder about what will happen when we use IronRuby, too? We'll we
need repr methods for each language?
I also wonder if things like a generic list couldn't pick up a
reasonable default __repr__ for use in all DLR languages.
We're using the .NET languages in an educational environment for new
students. Currently, we're going to have to add the __repr__ to all
classes, and add subclasses to all types for which there isn't a
reasonable repr default. For example, there is a pretty big difference
between: "[255, 255, 128]" and "<List<GraphicsCore+Pixel> object at
0x000002C [List<GraphicsCore+Pixel>]>" or whatever it might be.
In any event, we have a work around now; thanks again!
-Doug
using System;
using IronPython.Runtime; // for ICodeFormattable
using Microsoft.Scripting.Runtime; // for CodeContext
public class TestMe : ICodeFormattable {
public virtual string __repr__(CodeContext context) {
return String.Format("<Hi Mom>");
}
}
> -----Original Message----- From: users-bounces at lists.ironpython.com
> [mailto:users-bounces at lists.ironpython.com] On Behalf Of Douglas S.
> Blank Sent: Wednesday, May 28, 2008 12:22 PM To: Discussion of
> IronPython Subject: Re: [IronPython] What C# methods do __repr__ and
> __str__ use?
>
> Anyone have an answer for this: I can't write C# code that has its
> own method for use in repr(). Am I missing something?
>
> -Doug
>
> Douglas S. Blank wrote:
>> On Fri, May 23, 2008 3:37 pm, Toni Alatalo said:
>>> Douglas S. Blank kirjoitti:
>>>> will use it? Likewise, what C# method is used in Python's
>>>> str(obj)?
>>>>
>>> i don't know, but would guess ToString() .. and wonder about it
>>> if it wasn't that but something else :o repr() can't guess, i
>>> don't think there's an equivalent in .net, but can very well be
>>> mistaken there.
>> It looks like the C# method __str__ is used for the Python method,
>> but in IP2B2 the C# method __repr__ is getting overwritten by an
>> auto generated method. Is that a bug, or is there something I'm
>> missing?
>>
>> $ ipy2 -i python/graphics.py
>>>>> pm = Pixmap() pm.getPixel(0,0) # What is
>>>>> this calling?
>> <Pixel object at 0x2B>
>>>>> pm.getPixel(0,0).__repr__() # This is repr() too
>> '<GraphicsCore+Pixel object at 0x2C [GraphicsCore+Pixel]>'
>>>>> pm.getPixel(0,0).__Repr__() # This is mine
>> '<Pixel at (0,0)>'
>>>>> pm.getPixel(0,0).__str__() # That is mine, and str()
>>>>>
>> '<Pixel at (0,0)>'
>>>>> pm.getPixel(0,0).ToString() # Used by __repr__
>> 'GraphicsCore+Pixel'
>>
>> -Doug
>>
>>>> -Doug
>>>>
>>> ~Toni
>>>
>>> _______________________________________________ Users mailing
>>> list Users at lists.ironpython.com
>>> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>>>
>>
>
> _______________________________________________ Users mailing list
> Users at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
> _______________________________________________ Users mailing list
> Users at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>
More information about the Ironpython-users
mailing list