
Ask HN: Website listing/explaining common Python pitfalls&tricks? - zepearl
Is there any site which has a good list of things that programmers that come from other languages have to pay attention to, respectively, that lists some (useful) tricks that are commonly used?<p>Examples:<p>1) I think that I remember (I might be wrong) that once I was passing a &quot;list&quot; to a function and that when I manipulated the &quot;list&quot; in the function itself (by removing&#x2F;adding items) the &quot;list&quot;-variable outside of the function got the same changes (I didn&#x27;t expect that as I thought that the list-variable would be copied when passing it to the function).<p>2) When using the &quot;multiprocessing&quot;-module I thought that I managed to create a shared &quot;list&quot;-variable that could be read by all sub-processes, by declaring it outside of the function which was linked to the sub-processes. I&#x27;m sure that technically it did work, but I&#x27;m not sure if it was just by luck or what (anyway I think that Python v3.8 now officially supports shared memory&#x2F;variables?).<p>3) I kept creating strings like &#x27;The value of the variable is &quot; + str(myvar) + &quot;blablah&quot;&#x27; and I read all the time that that was a performance problem but recently I read as well that that&#x27;s not the case anymore =&gt; conflict :(<p>4) I recently read in an article referenced in HN that doing &quot;my_str = str&quot; in a function and that then using &quot;my_str()&quot; instead of &quot;str()&quot; is faster (somehow because &quot;my_str()&quot; is then &quot;local&quot;, but didn&#x27;t understand the exact implications) =&gt; is this something normal to do?<p>Thanks :)
======
eesmith
#1 is in the Python Programming FAQ at
[https://docs.python.org/3/faq/programming.html#why-did-
chang...](https://docs.python.org/3/faq/programming.html#why-did-changing-
list-y-also-change-list-x) though not exactly the context you mentioned.

#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...](https://docs.python.org/3/faq/programming.html#what-is-the-most-
efficient-way-to-concatenate-many-strings-together) . 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...](https://wiki.python.org/moin/PythonSpeed/PerformanceTips#Local_Variables)
.

> 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.

~~~
zepearl
Thank you

------
stummjr
This repo may be helpful to you:
[https://github.com/satwikkansal/wtfpython](https://github.com/satwikkansal/wtfpython)

It’s a collection of snippets to explain behaviors that may be considered
unexpected.

~~~
zepearl
Thank you

------
blondin
well some of what you describe come with experience but i found this guide a
while ago and found it really interesting:
[https://docs.quantifiedcode.com/python-anti-
patterns/](https://docs.quantifiedcode.com/python-anti-patterns/)

~~~
zepearl
Thank you

