[IronPython] Printing an object out as a string

Jeff Slutter jslutter at reactorzero.com
Wed Jan 28 05:41:10 CET 2009


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



More information about the Ironpython-users mailing list