r/Python • • 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

629 comments sorted by

View all comments

Show parent comments

2

u/brontide Nov 14 '17 edited Nov 14 '17

multiprocessing, not threading

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.

      # rather than
      try:
          foo = mydict['mykey']
      except: # note we generally forget to clarify the exception here
          foo = default_value
      # do this
      foo = mydict.get('mykey', default_value)
  • Don't forget collections

      from collections import defaultdict
      counts = defaultdict(int)
      counts['foo'] += 1   # look ma, no KeyError!
  • 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.

      # rather than
      try:
          os.remove(filename)
      except IOError:
          pass
      # Use
      with suppress(IOError):
          os.remove(filename) # If we don't care about the file being gone already

1

u/[deleted] Nov 14 '17

defaultdict

oh that one is really good.

i didn't know .get() could do a default value. i might have to take advantage of that.

suppress

guilty as fuck. i will have to use that. but how would i use it in the context of doing a pass on only certain exceptions?

1

u/brontide Nov 14 '17

but how would i use it in the context of doing a pass on only certain exceptions?

That's the point suppress() is a pass on everything, but you can put any exception in the function and it will only pass those specific ones and others will pass though. So suppress(IOError) will only catch IOError and KeyboardInterrupt will continue up the chain.

1

u/masklinn Nov 15 '17

Also wrt dict utility methods, setdefault is an excellent halfway point when you don't really want a defaultdict but don't always want to deal with KeyError by hand.