Skip to content

__rmul__ did not act as expected #214

Closed
@songjhaha

Description

@songjhaha

Here is a small demo to reproduce the problem:

using PythonCall
struct MyArray
    x::Array
end
function Base.:*(a::MyArray, b::AbstractArray)
    println("calling *(MyArray, AbstractArray)")
    return a.x * b
end
function Base.:*(a::AbstractArray, b::MyArray)
    println("calling *(AbstractArray, MyArray)")
    return a * b.x
end
a = [1 2; 3 4]
b = MyArray([4 3; 2 1])
a * b
# [8 5; 20 13]

pya = pyjlraw(a) # try not to use pya.__mul__()
pyb = pyconvert(Py, b)

# it's calling pyb.__rmul__(pya)
pya * pyb
# calling *(MyArray, AbstractArray)
# Python ArrayValue: <jl [13 20; 5 8]>

looks like pyb.__rmul__(pya) is calling *(MyArray, AbstractArray), so gets wrong result

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