0

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.

Christabella Irwanto
  • 1,161
  • 11
  • 15

1 Answers1

1
>>> import clr
>>> from System import Converter
>>> c = Converter[int, str](lambda i: str(i))
>>> c(42)
'42'
LOST
  • 2,956
  • 3
  • 25
  • 40
  • If you have a new question, please ask it by clicking the [Ask Question](https://stackoverflow.com/questions/ask) button. Include a link to this question if it helps provide context. - [From Review](/review/low-quality-posts/27662105) – RïshïKêsh Kümar Nov 20 '20 at 07:25
  • @RïshïKêshKümar this code snippet is an answer to the posed question. – LOST Nov 20 '20 at 08:26
  • 1
    @LOST wow, I tried this but didn't specify the types `[int, str]`! Maybe even more useful for me than your answer is *how* you got to this answer, if I may ask you to walk me through your process of figuring this out? – Christabella Irwanto Nov 20 '20 at 11:22
  • 1
    @ChristabellaIrwanto , well, I am one of maintainers of Python.NET. Basically the idea is that from Python lambda it is, generally speaking, impossible to figure out the return type. So you have to [specify type parameters explicitly](https://pythonnet.github.io/#using-generics) – LOST Nov 20 '20 at 21:47