[IronPython] What C# methods do __repr__ and __str__ use?

Douglas S. Blank dblank at brynmawr.edu
Thu May 29 00:21:38 CEST 2008


On Wed, May 28, 2008 6:07 pm, Michael Foord said:
> Douglas S. Blank wrote:
>> On Wed, May 28, 2008 5:19 pm, Dino Viehland said:
>>
>>> We could definitely add a __repr__ for the normal .NET lists (and even
>>> other standard types like Dictionary).  Maybe something like Python's
>>> set's repr so it'd look like:
>>>
>>> List[object]([2, 3, 4])
>>>
>>> Does that look good?
>>>
>>
>> For our particular usage (new students using IronPython as their first
>> language) this doesn't look so good. But if there will be a way that we
>> can write C# code which can be loaded by Ruby, Python, etc and we can
>> override the repr, then we can adapt.
>
> The advantage of this representation is that it is exactly the same
> syntax you use to create it - which I would have thought ideal for a
> learning situation.

Yes, indeed. However, on the first day of class when students do:

>>> getRGB(pixel)
List[object]([2, 3, 4])

we've just lost the battle :( I completely appreciate the repr for generic
list, as it is symmetric---it allows you to cut and paste to go back and
forth.

I think the answer for us is to make sure that we have getRGB() return a
native Python/Ruby list.

Thanks all, for helping us come up with the right answer!

-Doug

> Michael Foord
>
>> Currently, we're just using Python's list rather than the normal .NET
>> generic list type. That works great (for Python). Will there be a list
>> that will look native across dynamic languages?
>>
>> -Doug
>>
>>
>>> -----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 2:08 PM
>>> To: Discussion of IronPython
>>> Subject: Re: [IronPython] What C# methods do __repr__ and __str__ use?
>>>
>>> Dino Viehland wrote:
>>>
>>>> The problem w/ a common __repr__ is that each language will
>>>> presumably have different syntax for creating the object.  For
>>>> example a better repr in Python might be:
>>>>
>>>> Pixel(255,255,128)
>>>>
>>>> Where Ruby should probably display something like:
>>>>
>>>> Pixel.new(255,255,128)
>>>>
>>> Agreed. But maybe there can be some name *patterns* that if implemented
>>> once would suffice for most languages. I'm not too worried about this
>>> though.
>>>
>>>
>>>> Having a repr of [255,255,128] isn't that meaningful because if I
>>>> copy and paste that into my code I get a list instead of a Pixel
>>>> back.
>>>>
>>> Sorry, I switched issues on you :) That was the point of that
>>> example...
>>> generic lists don't look like lists at all. I'm actually more concerned
>>> with this example than the previous. A generic list of ints, strings,
>>> etc. ought to be able to have a better representation in IP.
>>>
>>>
>>>> We could revisit what our default repr does for .NET objects
>>>> that override ToString.  We discussed it once on the list long ago
>>>> but that was mainly focused on dealing w/ multi-line ToStrings if I
>>>> recall correctly - the thought of playing better with multiple
>>>> languages wasn't much of an issue back then for us.
>>>>
>>>> So basically I am open to suggestions :)
>>>>
>>> Perhaps generic lists could inherit a version of Python List's
>>> __repr__?
>>> (But of course, not just for Python, but for Ruby, etc.)
>>>
>>> Thanks!
>>>
>>> -Doug
>>>
>>>
>>>> -----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 1:47 PM To: Discussion of
>>>> IronPython Subject: Re: [IronPython] What C# methods do __repr__ and
>>>> __str__ use?
>>>>
>>>> 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
>>>>>
>>>>>
>>>> _______________________________________________ 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
>>> _______________________________________________
>>> Users mailing list
>>> Users at lists.ironpython.com
>>> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>>>
>>>
>>
>>
>>
>
>
> --
> http://www.ironpythoninaction.com/
> http://www.theotherdelia.co.uk/
> http://www.voidspace.org.uk/
> http://www.ironpython.info/
> http://www.resolverhacks.net/
>
> _______________________________________________
> Users mailing list
> Users at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>


-- 
Douglas S. Blank
Associate Professor, Bryn Mawr College
http://cs.brynmawr.edu/~dblank/
Office: 610 526 6501




More information about the Ironpython-users mailing list