r/Python • u/RickSore • Nov 14 '17
Senior Python Programmers, what tricks do you want to impart to us young guns?
Like basic looping, performance improvement, etc.
1.3k
Upvotes
r/Python • u/RickSore • Nov 14 '17
Like basic looping, performance improvement, etc.
2
u/brontide Nov 14 '17 edited Nov 14 '17
Yes!
While there are some things that work in threading you have to be super careful since the edge cases suck hard. Even stupid things like threads cannot be canceled should be a red flag for anything but the most trivial use-case.
I will add some of my own
dict.get(key, DEFAULT)! for those time you want to have a default value returned when the key does not exist.
Don't forget collections
VALIDATE ASSUMPTIONS: I recently ran smack into a "bug" ( unexpected behavior ) reported in 2009 against multiprocessing.Queue. I wrote a test program to validate the issue and then was able to work around it.
Don't nest, but do use comprehensions. They are a womderful additional to the language.
If you can't use with blocks or it's too messy remember this is Python just head over to contextlib and grab yourself an ExitStack for a complex function requiring a lot of context aware objects.
Speaking of contextlib you also have suppress.