Examples:
1) I think that I remember (I might be wrong) that once I was passing a "list" to a function and that when I manipulated the "list" in the function itself (by removing/adding items) the "list"-variable outside of the function got the same changes (I didn't expect that as I thought that the list-variable would be copied when passing it to the function).
2) When using the "multiprocessing"-module I thought that I managed to create a shared "list"-variable that could be read by all sub-processes, by declaring it outside of the function which was linked to the sub-processes. I'm sure that technically it did work, but I'm not sure if it was just by luck or what (anyway I think that Python v3.8 now officially supports shared memory/variables?).
3) I kept creating strings like 'The value of the variable is " + str(myvar) + "blablah"' and I read all the time that that was a performance problem but recently I read as well that that's not the case anymore => conflict :(
4) I recently read in an article referenced in HN that doing "my_str = str" in a function and that then using "my_str()" instead of "str()" is faster (somehow because "my_str()" is then "local", but didn't understand the exact implications) => is this something normal to do?
Thanks :)
#2 is because multiprocessing forks, and each Python fork has a copy of the original environment, so that list isn't so much shared as copied into all forked subprocesses. This is not specifically a Python pitfall as it is generally true of any language which support forking.
#3 is in the same FAQ, at https://docs.python.org/3/faq/programming.html#what-is-the-m... . There is a special optimization in CPython which tries to work around this common issue.
Note that if at all possible you should use f-strings, as f"The value of the variable is {myvar} blahblah" is the fastest option if there are a fixed number of strings.
#4 is mentioned in the "PythonSpeed/PerformanceTips" document at https://wiki.python.org/moin/PythonSpeed/PerformanceTips#Loc... .
> The final speedup available to us for the non-map version of the for loop is to use local variables wherever possible. If the above loop is cast as a function, append and upper become local variables. Python accesses local variables much more efficiently than global variables.
It’s a collection of snippets to explain behaviors that may be considered unexpected.