首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >python类中运行Julia方法的julia函数

python类中运行Julia方法的julia函数
EN

Stack Overflow用户
提问于 2021-05-28 05:33:27
回答 1查看 163关注 0票数 3

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

我尝试了不同的事情,得到了不同的错误,要么创建类,要么调用方法或其他错误。

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

这是我正在使用的代码。

代码语言:javascript
复制
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中的打印(这是一个字符串)。

有人能帮我把它修好吗?

提前谢谢你

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-05-28 09:28:02

这一切似乎都是可行的,只是在错误的地方有一个end,应该是String而不是string

代码语言:javascript
复制
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
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67733379

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档