[Python.NET] RE: example of calling Python from C# with simple object marshalling

MBarclay at cri-inc.com MBarclay at cri-inc.com
Wed Nov 19 16:20:06 EST 2003

Yes, overloading Invoke() for PyObjects sounds like a better solution than
adding a Portal class. That is so much simpler.
It's complete signature should probably be:

public object[] Invoke(params object[] objects)

This way, with a single method, you can pass down to Python nothing, a
single primitive value, or a bunch of them (0, 1, *).

Regarding your first overload (object Invoke(...) as opposed to object[]
Invoke(...)), I don't believe you can overload by return type alone (maybe
I'm thinking in C++), but that would be fine because the overload you
indicated that returns an object is less important than the one that
returns an object list (with the latter you can achieve what the former
does, with the additional utility of being able to return an empty object
list to mean "null" where a primitive type would otherwise be expected).

The other method...

object ToObject();

would be worth having as publics also, in fact I suppose it would nicely
encapsulate what the new Invoke() method(s) need to do their internal

Just a nitpick: I don't think you can compile:

int i = thing.ToObject() as Int32

...since "i" is a value type, and the 'as' operator can only be used on
reference types (value types cannot hold null). But I get your point...
int i = (Int32)thing.ToObject(); // this will throw an exception if the
conversion is not valid

Mark Barclay
Sr. Software Engineer
CRi Inc.

                    "Brian Lloyd"                                                                              
                    <brian at zope.co       To:     <MBarclay at cri-inc.com>                                        
                    m>                   cc:     <pythondotnet at python.org>                                     
                                         Subject:     RE: example of calling Python from C# with simple object 
                    11/19/2003            marshalling                                                          
                    02:50 PM                                                                                   

I think this points out an area still wanting in the embedding
interface (calling into Python and getting back managed objects
rather than PyObject wrappers).

I'm thinking that a good start would be a variation on what your
example does as an overload for the Invoke method of PyObjects:

  object Invoke(...)
  object[] Invoke(...)

I think it would also be a good idea to have a 'ToObject' method on

  object ToObject();

which would:

  - if the Python object represents a managed object, return
    that object

  - if the Python object is a primitive type that is convertible
    to a managed primitive type, return the converted value

  - otherwise throw an exception

Another option could be to have PyObject implement IConvertible,
though that would be more work, and might not buy you anything
over using:

int i = thing.ToObject() as Int32


Brian Lloyd        brian at zope.com
V.P. Engineering   540.361.1716
Zope Corporation   http://www.zope.com

More information about the PythonDotNet mailing list