3

I'm learning julia (v1.6) and I'm trying to create a julia function to run a julia method from a python class (pycall equivalent) where the method is a print.

I've tried different things and I get different errors either creating the class or calling the method or others.

https://github.com/JuliaPy/PyCall.jl (as a reference)

This is the code I'm using.

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

expected output is the equivalent to print('This is a string') in python.

Can somebody help me to make it work please?

Thank you in advance

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
jfcabreras
  • 141
  • 1
  • 1
  • 9

1 Answers1

3

This all seems to work except that you have one end in wrong place and it should be String not 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
Przemyslaw Szufel
  • 40,002
  • 3
  • 32
  • 62