pyjulia icon indicating copy to clipboard operation
pyjulia copied to clipboard

Convenient syntax for `setproperty!`

Open MilesCranmer opened this issue 2 years ago • 0 comments

I couldn't find an earlier issue but this seems like something that would be a common issue, so please share if there's a related one.

Basically, I think the following code should work:

from julia import Main as jl

jl.eval("""
    mutable struct MyStruct
        x::Int
    end
""")

obj = jl.MyStruct(1)
obj.x = 2

However, it gives the following error:

Traceback (most recent call last):
  File "/Users/mcranmer/Documents/ManimAnimation/SR/setting_demo.py", line 10, in <module>
    obj.x = 2
AttributeError: 'PyCall.jlwrap' object has no attribute 'x'

I couldn't completely figure out where __setattr__ should be overloaded, but I feel like it would be possible to enable this. For the record, __getattr__ seems to work fine; it's just __setattr__ that doesn't work.

As a temporary workaround, I am using the function:

from julia import Main as jl
jl.eval("""
    import Base: setproperty!
    setproperty!(obj, field::String, val) = setproperty!(obj, Symbol(field), val)
""")

set = jl.setproperty_b

jl.eval("""
    mutable struct MyStruct
        x::Int
    end
""")

obj = jl.MyStruct(1)
set(obj, "x", 2)

print(obj.x)
# 2

But I think obj.x = 2 should do this automatically.

(needed to overload setproperty! - see https://github.com/JuliaPy/PyCall.jl/issues/11)


cc @mkitti @marius311 @stevengj

MilesCranmer avatar Mar 27 '23 00:03 MilesCranmer