[Python.NET] Dynamic types of returns PyObject from the runtime

Pierre-Yves Strub pierre-yves at strub.nu
Tue Jan 29 14:23:42 CET 2013


Hi,

Not going to a jargon war, I was not speaking about the "dynamic type 
system" of C#, but the dynamic type of an object (or runtime type if you 
prefer) as opposed to its static one. Moreover, C# 4.0 is dynamically 
typed too:

namespace Test {
     class Program {
         static void plop(string _s) {
         }

         static void Main(string[] args) {
             dynamic foo = 0;

             foo.callMyMethod(0);
             Program.plop(foo);
         }
     }
}

Coming back to my initial mail, I would be surprised that returning the 
lowest type in the Python.Runtime.Py* hierarchy adds such an overhead 
(meaning, it only has to happen when crossing the Python -> .NET border 
and is a single switch - for instance PyQt is doing such a work and does 
not suffer from performance problems).

Anyway, I would be perfectly happy with some (kind of) downcast function 
in PyObject that I could call on the returned managed python values. 
Currently, I added such a function using F# type extensions, but I can 
create patches against the current trunk if you accept them.

Pierre-Yves.

On 01/29/2013 12:03 PM, Barton wrote:
> python dynamically typed: Yes
> C# dynamically typed: No, strongly typed. It uses lots of overloads to
> return the required type.
> I suppose some embedder somewhere has a neat way of applying the type
> info on the PyObject to some sort of wrapper.
>
> What language is that, anyway? It looks sort of interesting.
> Barton
>
> On 01/28/2013 08:05 AM, Pierre-Yves Strub wrote:
>> Hi,
>>
>> While playing with the .NET python runtime (see code bellow), I have
>> been surprised to see that the dynamic type of all values returned by
>> the runtime is PyObject. For instance, in the latter example, I would
>> expect "sum" to be of dynamic type PyInt and to be able to write (sum
>> :?> PyInt) instead of (PyInt.AsInt sum). Is it a "feature" of the
>> runtime binding ?
>>
>> let entry () =
>>     PythonEngine.Initialize ()
>>     use lock    = new PythonEngineLock () in
>>     use builtin = PythonEngine.ImportModule ("__builtin__") in
>>     use data    =
>>         new PyList
>>             (data |> Array.map (fun i -> new PyInt (i) :> PyObject)) in
>>     use sum     = builtin.GetAttr("sum").Invoke([|data :> PyObject|]) in
>>
>>         printfn "%A" (sum.GetType ());
>>         printfn "%A" (sum.GetPythonType ())
>>
>>
>> Best,
>>     Pierre-Yves.
>> _________________________________________________
>> Python.NET mailing list - PythonDotNet at python.org
>> http://mail.python.org/mailman/listinfo/pythondotnet
>>
>



More information about the PythonDotNet mailing list