Can you use LINQ types and extension methods in IronPython?

后端 未结 4 1938
迷失自我
迷失自我 2021-01-30 13:28

Is it possible to use the LINQ types and extension methods in IronPython?

If so how? And also is there often more pythonic to do the same thing?

4条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-30 13:59

    In IronPython 2.7.1 you have clr.ImportExtensions for this use case.

    import clr
    clr.AddReference("System.Core")
    import System
    clr.ImportExtensions(System.Linq)
    
    # will print 3 and 4 :)
    [2, 3, 4].Where(lambda x: x != 2).ToList().ForEach(System.Console.WriteLine)
    

    A little background: IronPython 2.7 initially introduced this feature, but there was an issue which stopped it from being really usable.

提交回复
热议问题