1

Im not sure if im missing something out because i'm new to Python, but according to a few threads and docs that I've read like this one, from what i understand the numpy should change the array since im passing it by parameter. Thats not what is occurring in my code though.

class Graph(object):
def __init__(self):
    self.arr = self.createArray()


def insertVertice(self):
    ##SHAPE = (1,1) - (2,2)
    np.insert(self.arr, 0, 0, axis=1)

if I let the code like this and print "arr", the bidimensional array stays the same [[0 0] [0 0]] but if I do it like:

self.arr = np.insert(self.arr, 0, 0, axis=1)

It changes...[[0 0 0] [0 0 0]]

Does anyone have a clue on what am I missing here?

3
  • the documentation clearly states that Note that insert does not occur in-place: a new array is returned. Commented Apr 5, 2022 at 7:04
  • Thanks. Indeed, I've read that, I just wasn't really feeling secure by make an attribution to the same value that I'm passing by parameter. Commented Apr 5, 2022 at 7:11
  • 1
    That's a very common usage, you should get use to it. Here is a link to the return statement best practices, maybe you'll find more information and learn what was missing here ! Commented Apr 5, 2022 at 7:17

1 Answer 1

1

In the doc you linked, it is clearly specified :

Return value: out [ndarray] A copy of arr with values inserted. Note that insert does not occur in-place: a new array is returned. If axis is None, out is a flattened array.

That means you have to use the return value as you are doing in the second example, the operation is not on the input array directly.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.