import syntax

Tim Chase python.list at tim.thechases.com
Mon Jul 29 16:14:45 EDT 2013


On 2013-07-29 15:48, Devyn Collier Johnson wrote:
> The PEP8 recommends importing like this:
> 
> import os
> import re
> 
> not like this:
> 
> import os, re
> 
> Why is that? Is there a performance advantage to one of the styles?

While I don't believe there's much of a performance difference (if
so, it should be pretty negligible, especially since imports are
usually just done at load-time rather than run-time), but

1) it's easier to read one-per-line (IMHO), particularly if you sort
them alphabetically

2) it makes for cleaner diffs

Which would you rather read and try to figure out what's going on?

  =====================================
  --- before.py   2013-07-29 15:04:38.250996094 -0500
  +++ after.py    2013-07-29 15:04:44.026996132 -0500
  @@ -1 +1 @@
  -import csv, re, sys, os
  +import csv, re, sys, glob, os
  =====================================

vs.

  =====================================
  --- before.py   2013-07-29 15:13:13.050997907 -0500
  +++ after.py    2013-07-29 15:13:18.434997950 -0500
  @@ -1,4 +1,5 @@
   import csv
  +import glob
   import os
   import re
   import sys
  =====================================

The latter makes it much easier (at least for me) to spot that "glob"
was added.  And it's more tolerant of merge-conflict resolution.

-tkc







More information about the Python-list mailing list