0

I'm writing Python functions to process very large images and I'm trying to minimize memory footprint when possible. My question is this : when I pass an image((a numpy ndarray) to a processing function, does it make a copy of the image during processing? For example in the following code, while I call subtract_value, will the function make a copy and increase memory usage?

def subtract_value(input, value):  # this is defined in another module
    input = input - value
    return input

img = subtract_value(img, value)   # this statement is part of a function

I understand if I write nested function I can definitely avoid this (Am I correct, by the way? ) but I'd like to make the function reusable by other programs.

def subtract_value(value):
    nonlocal img
    img = img - value

subtract_value(value)
user3667217
  • 2,172
  • 2
  • 17
  • 29
  • Objects are never copied when you are calling a function. They are passed by reference. – thefourtheye Sep 10 '15 at 05:27
  • No copy is made. Read [this question](http://stackoverflow.com/questions/986006/how-do-i-pass-a-variable-by-reference) for much more info. – BrenBarn Sep 10 '15 at 05:28
  • There is no "pass by value" or "pass by reference" in Python. All names point to objects, and you are simply passing along the names; not copies of the data. – Burhan Khalid Sep 10 '15 at 05:28
  • please consider reading this short text Other languages have "variables", Python has "names" http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#other-languages-have-variables. Think about "names" and "objects" instead of "variables" and "references" , for mutable and immutable sequenc please refer. http://codehabitude.com/2013/12/24/python-objects-mutable-vs-immutable/ – Pavan Gupta Sep 10 '15 at 05:54

0 Answers0