Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling a .Net function in Python which has a reference parameter

I'm using IronPython in VS2012 and trying to call a .Net function which takes in a Ref parameter,

Lib.dll

public int GetValue(ref double value)
{
  ...
}

Python:

import clr
clr.AddReference('Lib.dll')
from LibDll import *

value =0.0
x = GetValue(value)

am I missing something, in C# we use ref along with the variable name, what about here in Python?

like image 750
SanVEE Avatar asked Nov 18 '14 16:11

SanVEE


People also ask

How do you pass a parameter by reference in Python?

What is Pass by Reference In Python? Pass by reference means that you have to pass the function(reference) to a variable which refers that the variable already exists in memory. Here, the variable( the bucket) is passed into the function directly.

Is call by reference possible in Python?

When Mutable objects such as list, dict, set, etc are passed as arguments to the function call, then it can be considered as Call by reference in Python. This is because when the values are modified within the function then the change also gets reflected outside the function.

Is Python function pass by reference or pass by value?

Python passes arguments neither by reference nor by value, but by assignment.


1 Answers

There are two ways you can invoke methods with out or ref parameters from IronPython.

In the first case the call is handled by automatic marshalling. The return value and changed refs are wrapped in a tuple and (while having 15.1 as an example double to be passed) can be used like:

(returned, referenced) = GetValue(15.1)

The more explicit way is providing a prepared clr-reference:

refParam = clr.Reference[System.Double](15.1)
result = GetValue(refParam)
like image 172
Simon Opelt Avatar answered Sep 22 '22 23:09

Simon Opelt