0

In C++, it's good practice to pass references to large data structures to avoid the overhead of copying the whole thing by value. Python works very differently, though, and I'm wondering how best to pass a large data structure.

Here's the simple version. Is this what I should use, or is there something better?

foo(hugeArray):
    # Change values in hugeArray
    return hugeArray

hugeArray = foo(hugeArray)

Solution (Kudos to jonrsharpe!)

foo(hugeArray)
    hugeArray[0] = "New!"

myArray[0] = "Old."
foo(myArray)
print myArray[0] # Prints "New!"

The function changes the original array, so there's no need to assign the return value to the original to change it.

Qaz
  • 1,556
  • 2
  • 20
  • 34
  • 5
    If `foo` mutates `hugeArray` you would usually (implicitly or explicitly) `return None`, and simply call `foo(hugeArray)`. See e.g. `list.append`. As Python passes references (see e.g. [here](http://stackoverflow.com/a/986145/3001761)), there is no performance implication to returning `hugeArray`, though. – jonrsharpe Aug 08 '14 at 16:52
  • For "huge arrays" I would look into [generators](https://wiki.python.org/moin/Generators) if at all possible. If not, then look into the numpy's arrays. – James Mertz Aug 08 '14 at 16:53
  • Have you looked at generators in python? If you are worried about performance implications while passing a large array, generators could be useful. – Santanu C Aug 08 '14 at 17:07
  • @SantanuC If I were truly concerned about performance, I wouldn't use Python. :P Generators look fascinating, though, and I'll definitely read up. – Qaz Aug 08 '14 at 17:12
  • @KronoS Arrays are just an example, but the code I'm working on does indeed use numpy for them, and I'll definitely look into generators. Thanks! – Qaz Aug 08 '14 at 17:21

2 Answers2

1

Python will do this for you, unless you explicitly copy it or modify.

foo(hugeArray):
    # Change values in hugeArray
    return hugeArray

hugeArray2 = foo(hugeArray)
print("Was Python Clever? - ", hugeArray2 is hugeArray)

Note that if you want to re-assign hugeArray without making a copy, you can use slicing. so

hugeArray[:] = hugeNewArray

Instead of

hugeArray = hugeNewArray
ideasman42
  • 42,413
  • 44
  • 197
  • 320
  • Not sure what you mean, `hugeArray[:] = hugeNewArray` fully replaces `hugeArray`'s contents, not a shallow copy. – ideasman42 Aug 08 '14 at 17:30
1

No need to copy stuff

def foo(array):
    array[0] = 3  # do stuff to the array in place

huge_array = [1, 2, 3]
foo(huge_array)    # returns None
print(huge_array)  # [3, 2, 3]
Boris Verkhovskiy
  • 14,854
  • 11
  • 100
  • 103