Julia函数,用于在Python类中运行Julia方法

3

我正在学习Julia(v1.6),并尝试创建一个Julia函数来运行Python类中的Julia方法(与pycall等效),其中该方法为print。

我已经尝试了不同的方法,但是要么创建类出错,要么调用方法出错,或者其他错误。

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中打印('This is a string')。

有谁可以帮我让它正常工作吗?

提前感谢你。

1个回答

3

这看起来都是可以运行的,除了你有一个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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接