The program I’m coding now makes a pretty huge list of data items.
Now, I can make this list to be global (make available for other functions in other modules) and can be used in all other modules. Or, I can also pass them as a function arguments for the functions in the modules.
Note that, this huge array I’m talking about is not going to get modified in the functions in other modules, they just read data and use it for calculations and data stats etc.
So, of the two methods which has least memory consumption?
If by passing into the functions, if the language makes a local duplicates of the huge list even if the functions doesn’t modify it .. that’ll be doubling of the memory consumption which is not good thing. If this happens, I can make it global and use it. I got this doubt on memory management of python because when I once wrote a toy language, I included this particular issue.. i.e the argument data gets duplicated only if its edited .. else, it’ll always be pointed to the original data.
Firstly, there’s no such thing as a ‘global’ variable in Python (in the sense that it’s automatically available to all modules).
Secondly, Python doesn’t duplicate objects when passing to a function. Python variables are really just names that point to objects – when you pass a variable to a function, all that happens is that the function creates a new name that points to the original object. You can read or modify the contents of that object without any copies being made. (Note that if you rebind the name to a different object, the original reference is not changed.)