Can I use Named and Optional Arguments in ironpython

て烟熏妆下的殇ゞ 提交于 2020-01-04 03:24:07

问题


I hope to load .net dll in ironpython.

But one of static functions in .net dll, has some Named and Optional Arguments.

like, Draw(weight:w,height:h, Area=1)

Only can I use full arguments?


回答1:


Named and optional parameters are fully supported. .NET has had these for a long time for VB.NET support and so IronPython has supported that same way to do them since the beginning. The new C# syntax maps to the same underlying metadata as the old VB support.

For calling you use f(x = 42) which is Python's named parameter syntax. For optional parameters you can just leave them out. In your example case you can probably do Draw(weight, height) and leave Area out. Or you can call w/ weight and height as named parameters and leave Area out.

The underlying .NET meta data that IronPython looks for is either the OptionalAttribute or DefaultParameterValueAttribute. For optional we pass in default(T) unless the type is object in which case we pass in Missing.Value. This generally matches how reflection calls these APIs as well.




回答2:


As this SO question says, the new named arguments of .NET are not supported in IronPython (which uses named arguments to constructors it calls to set properties instead).

For optional ones, as this post says,

for optional parameters, like the arguments to the Address indexer, you can use Missing.Value to use the default.

after, of course, from System.Reflection import Missing.



来源:https://stackoverflow.com/questions/3384989/can-i-use-named-and-optional-arguments-in-ironpython

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!