Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PyJulia Custom Structure Dispatch #506

Open
McCastles opened this issue Oct 26, 2022 · 1 comment
Open

PyJulia Custom Structure Dispatch #506

McCastles opened this issue Oct 26, 2022 · 1 comment

Comments

@McCastles
Copy link

McCastles commented Oct 26, 2022

Hello, I've got a quistion as for the PyJulia functionality. How do I dispatch a Julia method for different objects defined in Python?

I assume the existence of an underlying mapping between native Python and Julia types: int -> Integer, float -> Float64, but at the same time np.array -> Array, which makes me think that I can customize this sort of mapping and connect MyClassPy -> MyClassJulia. Is that so?

Thank you in advance!

example.py

class MyClassA:
    pass

class MyClassB:
    pass

from julia import Julia
julia = Julia()
julia.eval("@eval Main import Base.MainInclude: include")
from julia import Main
Main.include("example.jl")

foo(MyClassA()) # "Dispatched to MyClassA version"

foo(MyClassB()) # "Dispatched to MyClassB version"

example.jl

# How can I access MyClassA and MyClassB types in Julia?
some_magic_way_to_include_structures_defined_in_python() 

function foo( custom_pyobject :: MyClassA )
    println("Dispatched to MyClassA version")
end

function foo( custom_pyobject :: MyClassB )
    println("Dispatched to MyClassB version")
end
@mkitti
Copy link
Member

mkitti commented Nov 23, 2022

Thr short answer is that you cannot do this. Use an if statement for the PyObject class.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants