JuliaPy / JuliaPy/pyjulia

Python variables are reallocated when called in pyjulia

Open
#428 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
893
Forks
101
PR merge metrics
No merged PRs in 30d

Description

e.g.
```python3
from julia import Main
from julia import Base
import numpy as np
a = np.random.rand(5)
print(a.data) #
Base.println(Base.pointer(a)) # Ptr{Nothing} @0x00007febe4cd3750
```
This means that a function that should update a variable inplace (here `dy`) does not
```python3
from julia import Main
import numpy as np
f_b = Main.eval(
"""
begin
function f_b(dy,y,a,b)
dy[1] = a*y[1]
dy[2] = b*y[2]
dy
end
end
"""
)
# Calling from python
dy = np.array([0,0])
y = np.array([1, 3])
print(dy) # returns [0 0]
print(f_b(dy, y, 5, 3)) # returns [5 9]
print(dy) # returns [0 0] (expected [5 9])
# Calling through Main
Main.dy = np.array([0,0])
Main.y = np.array([1,3])
print(Main.dy) # returns [0 0]
print(Main.f_b(Main.dy, Main.y, 5, 3)) # returns [5 9]
print(Main.dy) # returns [0 0] (expected [5 9])
```
Calling the function through `Main.eval` does update `dy`
```python3
Main.dy = np.array([0,0])
Main.y = np.array([1,3])
print(Main.dy) # returns [ 0 0 ]
Main.eval("f_b(dy, y, 5, 3)")
print(Main.dy) # returns [5 9]
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.