[IronPython] Printing an object out as a string

Curt Hagenlocher curt at hagenlocher.org
Wed Jan 28 18:37:02 CET 2009


If you're doing the logging from within C#, then there's no way to recover
the originally-passed Python object if the binder has had to perform a
conversion -- as it will have in this case.

On Tue, Jan 27, 2009 at 8:41 PM, Jeff Slutter <jslutter at reactorzero.com>wrote:

> Curt Hagenlocher wrote:
> > If you're willing to live on the bleeding edge and build from the latest
> > sources, it turns out that Tomas just added this functionality to the
> > hosting interfaces yesterday. ObjectOperations.Format(obj) should return
> > a string that matches what the REPL would have printed.
>
> I have no problem with living on the bleeding edge. But I realized why
> repr wasn't working so well for me:
>
> In a nutshell, I need to get the Python function call string that was
> made (including arguments), for logging purposes. The functions are in
> my C# code, the calls are made from Python.
>
> I can get the name of the function through some means, but the arguments
> are the part I've having a hard time with. I can make the assumption
> that the arguments being passed into the function have a textual
> representation.
>
> So, if I have a function in C# like:
>
> void MyFunc( IEnumerable<int> someList )
> {
>        string someListAsStr = SomeMagicFunction(someList);
>        Log( someListAsStr );
> }
>
>
> and in Python I call it like:
>
> a = [0,1,2]
> MyFunc( a )
>
>
> I want to log the string "[0,1,2]"
>
> repr wasn't working for me, I believe, because once I get inside MyFunc,
> IronPython has wrapped and converted the PythonList into this
> IEnumerable<int> I have. So, repr was giving me something not so pretty.
>
> Will the new Format function give me what I want? Or should I continue
> down a different path?
>
> Thanks,
> Jeff
> _______________________________________________
> Users mailing list
> Users at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20090128/33420701/attachment.html>


More information about the Ironpython-users mailing list