Call Python code from an existing project written in Swift

前端 未结 4 985
忘了有多久
忘了有多久 2020-11-27 17:26

I need a way to call Python code from Swift on an Apple platform. A library would be ideal. I\'ve done a considerable amount of Google searching, and the closest material I

4条回答
  •  野性不改
    2020-11-27 18:01

    In Swift 4.2 there was an approved feature to allow dynamic languages to be ported directly into swift

    https://github.com/apple/swift-evolution/blob/master/proposals/0195-dynamic-member-lookup.md

    Will look similar to:

      // import pickle
      let pickle = Python.import("pickle")
    
      // file = open(filename)
      let file = Python.open(filename)
    
      // blob = file.read()
      let blob = file.read()
    
      // result = pickle.loads(blob)
      let result = pickle.loads(blob)
    

提交回复
热议问题