# PyCall: Modifying an array in Python

**URL:** <https://discourse.julialang.org/t/pycall-modifying-an-array-in-python/1003>\
**Category:** General Usage\
**Tags:** pycall\
**Created:** [December 16, 2016, 9:05pm UTC](https://discourse.julialang.org/t/pycall-modifying-an-array-in-python/1003 "2016-12-16T21:05:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![cortner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cortner/32/204_2.png) [@cortner](https://discourse.julialang.org/u/cortner)\
**Post date:** [December 16, 2016, 9:05pm UTC](https://discourse.julialang.org/t/pycall-modifying-an-array-in-python/1003/1 "2016-12-16T21:05:08Z")

</div>

I ran into a problem that is reproduced with the following elementary snippet:

```julia
app = pyeval("lambda x: x.append(1)")
x = []
app(x)
@show x
# x = Any[]

```

Why is x not modified?

I am guessing that `append` creates a new block of data for the python object while Julia only keeps track of the original data? Can this be fixed?

---

<div class="post-metadata">

**Author:** ![ihnorton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihnorton/32/26_2.png) [@ihnorton](https://discourse.julialang.org/u/ihnorton)\
**Post date:** [December 16, 2016, 11:35pm UTC](https://discourse.julialang.org/t/pycall-modifying-an-array-in-python/1003/2 "2016-12-16T23:35:44Z")

</div>

> [@cortner](#):
>
> I am guessing that append creates a new block of data for the python object while Julia only keeps track of the original data?

Right, the argument `x` is converted to a Python representation using `PyObject(x)`, which makes a copy (because Python does not know how to use a Julia `Any` array – the underlying memory representations are different). This object is passed to the closure, appended-to, and promptly forgotten.

However, there are several object types which do have transparent sharing: [PyVector](https://github.com/JuliaPy/PyCall.jl#pyvector), and typed arrays (which will be converted to numpy arrays on the Python side, pointing to the same memory location as in Julia).

e.g. PyVector:

```julia
julia> x = PyVector([1,2,3])
...
julia> app(x)

julia> x
4-element Int64 PyVector:
 1
 2
 3
 1

```

---

<div class="post-metadata">

**Author:** ![cortner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cortner/32/204_2.png) [@cortner](https://discourse.julialang.org/u/cortner)\
**Post date:** [December 17, 2016, 6:10am UTC](https://discourse.julialang.org/t/pycall-modifying-an-array-in-python/1003/3 "2016-12-17T06:10:59Z")

</div>

Thank you for this. That works for me now. But one thing I don’t understand yet:

```julia
app = pyeval("lambda x: x.append(1)")
x = Float64[] 
app(x)

```

throws an error

```julia
ERROR: PyError (:PyObject_Call) <type 'exceptions.AttributeError'>
AttributeError("'numpy.ndarray' object has no attribute 'append'",)
  File "PyCall.jl", line 1, in <lambda>

```

But doesn’t `PyVector(x)` convert to a `numpy ` array as well? What is the difference?

---

<div class="post-metadata">

**Author:** ![simonbyrne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simonbyrne/32/19_2.png) [@simonbyrne](https://discourse.julialang.org/u/simonbyrne)\
**Post date:** [December 17, 2016, 8:20am UTC](https://discourse.julialang.org/t/pycall-modifying-an-array-in-python/1003/4 "2016-12-17T08:20:38Z")

</div>

> [@cortner](#):
>
> But doesn’t PyVector(x) convert to a numpy array as well? What is the difference?

No, it’s a wrapper of a Python list:

> **[GitHub - JuliaPy/PyCall.jl: Package to call Python functions from the Julia...](https://github.com/JuliaPy/PyCall.jl#pyvector)**
>
> Package to call Python functions from the Julia language - GitHub - JuliaPy/PyCall.jl: Package to call Python functions from the Julia language

---

<div class="post-metadata">

**Author:** ![cortner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cortner/32/204_2.png) [@cortner](https://discourse.julialang.org/u/cortner)\
**Post date:** [December 17, 2016, 2:08pm UTC](https://discourse.julialang.org/t/pycall-modifying-an-array-in-python/1003/5 "2016-12-17T14:08:40Z")

</div>

ah - thanks, I never noticed that `PyVector` and `PyArray` are quite different.
