我正在学习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中的打印(这是一个字符串)。
有人能帮我把它修好吗?
提前谢谢你
发布于 2021-05-28 09:28:02
这一切似乎都是可行的,只是在错误的地方有一个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 stringhttps://stackoverflow.com/questions/67733379
复制相似问题