About

Welcome to Panela, Matt Harrison's take on mostly Open Source, Linux, Python, innovation in those areas, other buzzwords and Dick Proenneke. It comes complete with the illustrations as needed. Note the opinions expressed here are merely my opinions and not the opinions of my employer.

about Matt

Calendar

««Nov 2009»»
SMTWTFS
1234567
8910
11
12
1314
151617
18
192021
22232425262728
2930

Mailing List

My RSS Feeds








Great post about what to do in Python module scope

posted 2009.10.19 Mon

Chris McDonough clearly explains what code is appropriate at the module level. In the spirit of "importing code shouldn't have weird side effects", here's his list:

  • An import of another module or global.
  • Assignment of a variable name in the module to some constant value.
  • The addition of a function via a def statement.
  • The addition of a class via a class statement.
  • Control flow which may handles conditionals for platform-specific handling or failure handling of the above.

And then this great quote: Any other sort of logic inside the top level execution path of a Python module (any code that would be executed during "import") should be regarded with great suspicion and perhaps even loathing.

Here here Chris! Perhaps you should start a Foundation, Crusaders against Suspicious (Python) code.

(I guess this is sort of a retweet, since I didn't want to login to comment)

links: digg this    del.icio.us    reddit




1. Eric left...
2009.10.28 Wed 10:43 am

This, of course, is precisely the trouble I've been having with Twisted. Oddly enough, they provide a neat way to install different reactors based on a configuration string, but it has to be used before you subclass anything useful.

(I tried to comment on the original post, but failed.)