Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #51486

Re: import syntax

Date 2013-07-29 15:14 -0500
From Tim Chase <python.list@tim.thechases.com>
Subject Re: import syntax
References <51F6C720.2020404@Gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.5262.1375128799.3114.python-list@python.org> (permalink)

Show all headers | View raw


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



Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: import syntax Tim Chase <python.list@tim.thechases.com> - 2013-07-29 15:14 -0500

csiph-web