Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convenient syntax for setproperty! #527

Open
MilesCranmer opened this issue Mar 27, 2023 · 0 comments
Open

Convenient syntax for setproperty! #527

MilesCranmer opened this issue Mar 27, 2023 · 0 comments

Comments

@MilesCranmer
Copy link
Collaborator

MilesCranmer commented Mar 27, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant