Christabella Irwanto
Christabella Irwanto

Reputation: 1201

Converting a .NET type (System.Converter) to a Python type with Python.NET

I am trying to write the Python.NET equivalent of this C# code:

Util.ArrayInit(motifPositionData[i], j => backgroundDist.Sample());

However, the Util.ArrayInit() method accepts an int and a Converter<TInput,TOutput> Delegate --- of which I have no idea what the Python equivalent would be. I tried a lambda but it didn't work:

Util.ArrayInit(1, lambda x: 1)
TypeError: No method matches given arguments for ArrayInit: (<class 'int'>, <class 'function'>)

Question 1: How can I inspect which arguments do have a matching method, from the Python debugger/console? (Maybe not possible.)

Question 2: Would I need to create a custom object marshalling from a Python function (e.g. a lambda) to a .NET System.Converter, the way that Python int is automatically converted to a .NET System.Int32... Or is there as simpler way of doing this?


I have circumvented this by not using Util.ArrayInit in the first place, but am still curious about the case doing so is not as trivial.

Upvotes: 0

Views: 1381

Answers (1)

LOST
LOST

Reputation: 3269

>>> import clr
>>> from System import Converter
>>> c = Converter[int, str](lambda i: str(i))
>>> c(42)
'42'

Upvotes: 1

Related Questions