Skip to content

Allow arithmetic of jlwrap in Python #676

Closed
@marius311

Description

@marius311

If you're in PyJulia and grabbed some Julia object which has custom +/-/*/etc.. defined, it'd be nice if you can do that directly in Python. E.g.

In [1]: %load_ext julia.magic 
Initializing Julia interpreter. This may take some time...

In [2]: %julia using LinearAlgebra 

In [3]: x = %julia I 

In [4]: 2*x 
   ...:                                                                                                                             
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-4-cac53a56d728> in <module>
----> 1 2*x

TypeError: unsupported operand type(s) for *: 'int' and 'PyCall.jlwrap'

where that error is the current behavior but it would be nice if it instead returned:

<PyCall.jlwrap UniformScaling{Int64}
2*I>

I think I can probably implement this if you think you'd accept this functionality.

From a quick glance it looks like the way to proceed is to give the jlwrap object __add__, __mul__, etc... via some mechanism or another somewhere near https://github.com/JuliaPy/PyCall.jl/blob/master/src/pytype.jl#L379, either by adding to that if-clause, or something else. Am I on the right track?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions