[IronPython] Printing an object out as a string

Tomas Matousek Tomas.Matousek at microsoft.com
Wed Jan 28 05:14:40 CET 2009


If you need to call it from C# app you can use

engine.ObjectOperations.Format(obj)

where "engine" could be created like this:

ScriptEngine engine = IronPython.Hosting.Python.CreateEngine();

ObjectOperations::Format calls "repr".

Tomas

-----Original Message-----
From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Jimmy Schementi
Sent: Tuesday, January 27, 2009 7:56 PM
To: Discussion of IronPython
Subject: Re: [IronPython] Printing an object out as a string

repr([0,1,2])

> -----Original Message-----
> From: users-bounces at lists.ironpython.com [mailto:users-
> bounces at lists.ironpython.com] On Behalf Of Jeff Slutter
> Sent: Tuesday, January 27, 2009 7:53 PM
> To: Discussion of IronPython
> Subject: [IronPython] Printing an object out as a string
>
> Given an object, how can I go about printing out that object as a
> string
> representing the object in Python syntax?
>
> I need something a little more powerful than just object.ToString()
>
> I need something like how the interactive console reports the result of
> the statement.
>
> For instance:
>
> a = [0,1,2]
> b = [a,8,9]
>
> If I pass 'b' to a function in C#, like:
>
> void SomeFunction( object obj )
> {
>    string objStr = ConvertToString( obj );
>    Console.Write( objStr );
> }
>
> it would print out:
> [[0,1,2],8,9]
>
> Obviously ToString() will work for simple things, but once I try to use
> lists or dictionaries it no longer works.
>
> Thank you,
> Jeff
> _______________________________________________
> 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