Python Continuous Types in Function Calls

I have a sqlalchemy query that returns a tuple. I pass this tuple to a function, and since it is an immutable type, a new instance of the tuple is created in the called function.

How does python handle this in terms of memory management? Is the full copy of the tuple created, or does it use some kind of smart "write / null copy"?

The problem for me is that these source tuples can consume large amounts of memory, and just by calling a function to do some processing on them, Python will effectively double the memory consumption.

With the exception of writing inline code, how can I avoid such inefficiencies?

+2
source share
2 answers

When you call a function, only a reference to the tuple is passed, not a copy. The fact that it is immutable does not mean that it will be copied to a function call, you simply cannot change it.

+2
source

Python passes object references by value. Therefore, you do not need to worry about it.

0
source

All Articles