Why is this in-place operation not modifying the Python object in-place?
I would expect the outside function
ar
print to print the modified ar
, because the ar
is passed as object reference. But I got this:
6 Replies
Your signature needs to be
def do_numpy_stuff(inout ar: PythonObject) -> PythonObject:
. Note the addition of inout
. That means changes to ar
inside the function also show up outside the function. Otherwise you are working with a copy and as you see the changes don't get reflected.
I am not sure if this will always be the case for def
functions since the expectation in Python is that everything is inout
.I understand with
inout
it would be mutated. But inout
is when regarding a Mojo variable, one could reassign this variable (lvalue) to anything of the same type:
prints out
Any PythonObject would do. This is a separate point from what I was trying to show up there in the orignal post.
What I was expressing in the original post was more similar to when PythonObject
is a list. This would mutate the Python object internally.
I think this is a bug. Please consider file an issue on GH.
Hey yeah @tzl this mutation should definetly be visible in the caller side as well even without
inout
as it is in Python, if you can please raise an issueGitHub
[BUG] NumPy array in-place operation over a copied object reference...
Bug description from python import Python def do_numpy_stuff(ar: PythonObject) -> PythonObject: ar.iadd(3) print("inside function:\n", ar) return ar fn main() raises: var np = Pyth...
Thanks heaps for that @tzl