CSharp function with the out argument in Python

Hi, a python newbie here.

I am porting a CSharp app to python and have the line:

string sBlockName = GetInfo(iBlockID, out dLength, out dWidth);

iBlockID is passed to the function
sBlockName is the return value
dLength and dWidth are passed by reference using the out keyword.

How to write this in python?

Any help is appreciated! :handshake:

Please note that I don’t want to rewrite this function in Python since it is a library function and I only want to translate the above single line to Python.

I don’t know that we have enough information to say much. Python uses pass my reference for complex data types, so I wouldn’t expect a problem. What have you tried? In which ways has it failed?

Thank you @JeremyLT for looking into this.

I have tried

sBlockName, dLength, dWidth = GetInfo(iBlockId)

Also tried

dLength = 0
dWidth = 0
sBlockName, dLength, dWidth = GetInfo(iBlockId)

Declaring a tuple


This is also not working.

Declaring a list


This too is not working.

But what is inside the function?


You can imagine that the function looks up a database of Blocks that has columns like
Block_ID, Block_Name, Block_Length, and Block_Width.

So the function GetInfo Takes the ID and returns the Name of the block and also the length and the width of the block for the given ID.

I don’t know what’s inside the function.
It is a library function.

string sBlockName = GetInfo(iBlockID, out dLength, out dWidth);

I can use it in CSharp and it returns the block’s name sBlockName when I provide the block’s ID in BlockID.

Besides that, it also fetches the block’s length and width in the out variables dLength and dWidth.

As mentioned earlier in the main question, I do not want to re-write the function, I only want to write the equivalent line of code for the CSharp function call:

string sBlockName = GetInfo(iBlockID, out dLength, out dWidth);

Thanks again for looking into this.

So you are writing a Python wrapper around existing CSharp code? You need to look into ffi. I know how to do this for C, but I’d have to Google extensively to do it with CSharp .

Do you have some reason to suppose that just calling the CSharp function in Python with a different number of arguments should work? How is that function represented in Python? You can’t just call a CSharp function from Python without some serious legwork.

Without knowing much, much more, I have no way of telling what is going on.


Once again - No.
I am NOT writing a Python wrapper around a CSharp code.

The CSharp line of code simply serves as an example of how the LIBRARY FUNCTION is called or consumed in a CSharp program.

For Example, in VB.Net it will be called like this:

Dim iBlockID as Integer = 1207
Dim dLength as Double
Dim dWidth As Double

Dim sBlockName As String = GetInfo(iBlockID, dLength, dWidth)

The function signature or definition is:

Dim sBlockName As String = GetInfo(ByVal iBlockID As Integer, ByRef dLength As Double, ByRef dWidth As Double)

Hope this helps.
Thank you once again for looking into this issue so far :+1:

But I thought you were calling to a CSharp library for this function?

I’m so confused.

You need a way to replicate the action of this function. You can do this by either rewriting the function in Python or by calling the CSharp function from Python. Which do you want to do?


The library was perhaps written in C, I have no clue.
It is a DLL file and contains the function GetInfo.
It is a third-party library and I don’t have the source code.
Hence I cannot rewrite the function as I don’t know what happens inside the function.

Previously, I was using CSharp to write an app that calls this GetInfo function and it worked well.

Now the client wants to write another app in Python that also uses the same DLL library and the function GetInfo from that library.

I want to use that function from that DLL library in Python.

There are hundreds of other functions in that library that I can call in Python without issues.

Only this function GetInfo is not working properly.

I gave the CSharp syntax to provide information on how that function works in CSharp and its definition.

string sBlockName = GetInfo(iBlockID, out dLength, out dWidth);

Now, I want to write a Python equivalent of the above line.

How would you write the above line in Python?

Hope it is clear.
Any help is appreciated :pray:

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.