Convenient syntax for `setproperty!`
- Dominant language
- Python
- Stars
- 893
- Forks
- 101
- PR merge metrics
- No merged PRs in 30d
Description
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:
```python
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:
```python
Traceback (most recent call last):
File "/Users/mcranmer/Documents/ManimAnimation/SR/setting_demo.py", line 10, in
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:
```python
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
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.