Skip to content

Convenient syntax for setproperty! #527

@MilesCranmer

Description

@MilesCranmer

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 JuliaPy/PyCall.jl#11)


cc @mkitti @marius311 @stevengj

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