JuliaPy / PyCall.jl

Package to call Python functions from the Julia language
MIT License
1.46k stars 187 forks source link

Operator(+) overloading for PyObject #846

Open metab0t opened 3 years ago

metab0t commented 3 years ago

I port my following Python code into using PyPlot:

import PyPlot

const plt = PyPlot

fig, ax = plt.subplots(facecolor="w", edgecolor="k")

t = 1:24
x = 1:24
y= 24:-1:1
ax.plot(t,x, label="x")
ax2 = ax.twinx()
ax2.plot(t,y, label="y")

lines, labels = ax.get_legend_handles_labels()
lines2, labels2 = ax2.get_legend_handles_labels()
ax.legend(lines + lines2, labels + labels2, loc="upper left", fontsize=20, ncol = 1)

fig.tight_layout()
plt.show()

In lines + lines2, I encounter an error:

PyError ($(Expr(:escape, :(ccall(#= C:\Users\meta\.julia\packages\PyCall\BcTLp\src\pyoperators.jl:12 =# @pysym(:PyNumber_Add), PyPtr, (PyPtr, PyPtr), a, b))))) <class 'TypeError'>
TypeError("unsupported operand type(s) for +: 'Line2D' and 'Line2D'")

Stacktrace:
 [1] pyerr_check at C:\Users\meta\.julia\packages\PyCall\BcTLp\src\exception.jl:62 [inlined]
 [2] pyerr_check at C:\Users\meta\.julia\packages\PyCall\BcTLp\src\exception.jl:66 [inlined]
 [3] _handle_error(::String) at C:\Users\meta\.julia\packages\PyCall\BcTLp\src\exception.jl:83
 [4] macro expansion at C:\Users\meta\.julia\packages\PyCall\BcTLp\src\exception.jl:97 [inlined]
 [5] + at C:\Users\meta\.julia\packages\PyCall\BcTLp\src\pyoperators.jl:11 [inlined]
 [6] _broadcast_getindex_evalf at .\broadcast.jl:648 [inlined]
 [7] _broadcast_getindex at .\broadcast.jl:621 [inlined]
 [8] getindex at .\broadcast.jl:575 [inlined]
 [9] macro expansion at .\broadcast.jl:932 [inlined]
 [10] macro expansion at .\simdloop.jl:77 [inlined]
 [11] copyto! at .\broadcast.jl:931 [inlined]
 [12] copyto! at .\broadcast.jl:886 [inlined]
 [13] copy at .\broadcast.jl:862 [inlined]
 [14] materialize at .\broadcast.jl:837 [inlined]
 [15] broadcast_preserving_zero_d at .\broadcast.jl:826 [inlined]
 [16] +(::Array{PyObject,1}, ::Array{PyObject,1}) at .\arraymath.jl:47
 [17] top-level scope at In[177]:17
 [18] include_string(::Function, ::Module, ::String, ::String) at .\loading.jl:1091
 [19] execute_code(::String, ::String) at C:\Users\meta\.julia\packages\IJulia\rWZ9e\src\execute_request.jl:27
 [20] execute_request(::ZMQ.Socket, ::IJulia.Msg) at C:\Users\meta\.julia\packages\IJulia\rWZ9e\src\execute_request.jl:86
 [21] #invokelatest#1 at .\essentials.jl:710 [inlined]
 [22] invokelatest at .\essentials.jl:709 [inlined]
 [23] eventloop(::ZMQ.Socket) at C:\Users\meta\.julia\packages\IJulia\rWZ9e\src\eventloop.jl:8
 [24] (::IJulia.var"#15#18")() at .\task.jl:356

It seems that operator + is not appropriately overloaded for PyObject. I wonder that how to tackle this issue.