Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>PythonNet doesn't document this quite as clearly as IronPython, but it does almost the same thing.</p> <p>So, let's look at the IronPython documentation for <a href="http://ironpython.net/documentation/dotnet/dotnet.html#ref-and-out-parameters"><code>ref</code> and <code>out</code> parameters</a>:</p> <blockquote> <p>The Python language passes all arguments by-value. There is no syntax to indicate that an argument should be passed by-reference like there is in .NET languages like C# and VB.NET via the ref and out keywords. IronPython supports two ways of passing ref or out arguments to a method, an implicit way and an explicit way.</p> <p>In the implicit way, an argument is passed normally to the method call, and its (potentially) updated value is returned from the method call along with the normal return value (if any). This composes well with the Python feature of multiple return values…</p> <p>In the explicit way, you can pass an instance of <code>clr.Reference[T]</code> for the ref or out argument, and its Value field will get set by the call. The explicit way is useful if there are multiple overloads with ref parameters…</p> </blockquote> <p>There are examples for both. But to tailor it to your specific case:</p> <pre><code>itemIDs, itemNames = GetItems() </code></pre> <p>Or, if you really want:</p> <pre><code>itemIDsRef = clr.Reference[Array[int]]() itemNamesRef = clr.Reference[Array[String]]() GetItems(itemIDs, itemNames) itemIDs, itemNames = itemIDsRef.Value, itemNamesRef.Value </code></pre> <hr> <p>CPython using <a href="http://pythonnet.sourceforge.net">PythonNet</a> does basically the same thing. The easy way to do <code>out</code> parameters is to not pass them and accept them as extra return values, and for <code>ref</code> parameters to pass the input values as arguments and accept the output values as extra return values. Just like IronPython's implicit solution. (Except that a <code>void</code> function with <code>ref</code> or <code>out</code> parameters always returns <code>None</code> before the <code>ref</code> or <code>out</code> arguments, even if it wouldn't in IronPython.) You can figure it out pretty easily by inspecting the return values. So, in your case:</p> <pre><code>_, itemIDs, itemNames = GetItems() </code></pre> <p>Meanwhile, the fact that these happen to be arrays doesn't make things any harder. As <a href="http://pythonnet.sourceforge.net/readme.html#arrays">the docs</a> explain, PythonNet provides the iterable interface for all <code>IEnumerable</code> collections, and the sequence protocol as well for <code>Array</code>. So, you can do this:</p> <pre><code>for itemID, itemName in zip(itemIDs, itemNames): print itemID, itemName </code></pre> <p>And the <code>Int32</code> and <code>String</code> objects will be converted to native <code>int</code>/<code>long</code> and <code>str</code>/<code>unicode</code> objects just as if they were returned directly.</p> <hr> <p>If you really want to explicitly convert these to native values, you can. <code>map</code> or a list comprehension will give you a Python list from any iterable, including a PythonNet wrapper around an <code>Array</code> or other <code>IEnumerable</code>. And you can explicitly make a <code>long</code> or <code>unicode</code> out of an <code>Int32</code> or <code>String</code> if you need to. So:</p> <pre><code>itemIDs = map(int, itemIDs) itemNames = map(unicode, itemNames) </code></pre> <p>But I don't see much advantage to doing this, unless you need to, e.g., pre-check all the values before using any of them.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload