【问题标题】:Julia function to run julia method in python classJulia函数在python类中运行julia方法
【发布时间】:2021-05-28 05:33:27
【问题描述】:

我正在学习 julia (v1.6),我正在尝试创建一个 julia 函数来从 python 类(pycall 等效)运行 julia 方法,其中该方法是打印。

我尝试了不同的方法,但在创建类或调用方法或其他方法时遇到了不同的错误。

https://github.com/JuliaPy/PyCall.jl(作为参考)

这是我正在使用的代码。

using PyCall
# Python Class
@pydef mutable struct python_class
    function __init__(self, a)
        self.a = a
    end
    # Julia Method embeded in Python Class
    function python_method(self, a)
        println(a)
end

# Julia calling python class with julia method
function main(class::PyObject, a::string)
    # Instantiate Class
    b = class(a)
    # Call Method
    b.python_method(a)
end

a = "This is a string"

# Run it
main(python_class, a)

end

预期输出相当于python中的print('This is a string')。

有人可以帮我解决吗?

提前谢谢你

【问题讨论】:

    标签: python julia pycall


    【解决方案1】:

    这一切似乎都有效,只是你有一个 end 放错了位置,它应该是 String 而不是 string

    julia> @pydef mutable struct python_class
               function __init__(self, a)
                   self.a = a
               end
               # Julia Method embeded in Python Class
               function python_method(self, a)
                   println(a)
           end
    
           end
    PyObject <class 'python_class'>
    
    
    julia> function main(class::PyObject, a::String)
               # Instantiate Class
               b = class(a)
               # Call Method
               b.python_method(a)
           end
    main (generic function with 1 method)
    
    julia> a = "This is a string"
    "This is a string"
    
    julia> main(python_class, a)
    This is a string
    

    【讨论】:

      猜你喜欢
      • 2019-05-01
      • 2014-10-09
      • 1970-01-01
      • 1970-01-01
      • 2021-02-14
      • 1970-01-01
      • 2016-04-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多