Skip to content

Python Dos and Don'ts

Hannes Schmidt edited this page Dec 9, 2015 · 15 revisions
  • No need for threading.Event to signal boolean conditions between threads. Use threading.Event only if you need to efficiently block on the event to occur. Otherwise use a simple bool variable. The GIL takes care of the rest.

  • Don't use format() or % to format log messages. The logger's debug(), info(), warn(), etc. methods are all variadic, and the extra arguments will be interpolated. IOW,

    logger.info('Hello, %s' % world)
    

    is equivalent to

    logger.info('Hello, %s', world)
    

    just less efficient, if the log level set above info.

  • Don't waste pixels, use single-quoted strings by default.