Re: PEP 8 exegetics: conditional imports?

2009-08-08 Thread Steven D'Aprano
On Fri, 07 Aug 2009 16:50:12 +, kj wrote: I seek the wisdom of the elders. Is there a consensus on the matter of conditional imports? Are they righteous? Or are they the way of the wicked? Being an elder doesn't grant you wisdom. It just might mean you've been stupid and ignorant for

PEP 8 exegetics: conditional imports?

2009-08-07 Thread kj
Conditional imports make sense to me, as in the following example: def foobar(filename): if os.path.splitext(filename)[1] == '.gz': import gzip f = gzip.open(filename) else: f = file(filename) # etc. And yet, quoth PEP 8: - Imports are always put at the

Re: PEP 8 exegetics: conditional imports?

2009-08-07 Thread alex23
On Aug 8, 2:50 am, kj no.em...@please.post wrote: Conditional imports make sense to me, as in the following example[...] And yet, quoth PEP 8:     - Imports are always put at the top of the file, just after any module       comments and docstrings, and before module globals and constants.

Re: PEP 8 exegetics: conditional imports?

2009-08-07 Thread Albert Hopkins
On Fri, 2009-08-07 at 16:50 +, kj wrote: Conditional imports make sense to me, as in the following example: def foobar(filename): if os.path.splitext(filename)[1] == '.gz': import gzip f = gzip.open(filename) else: f = file(filename) # etc.

Re: PEP 8 exegetics: conditional imports?

2009-08-07 Thread Albert Hopkins
On Fri, 2009-08-07 at 16:50 +, kj wrote: Conditional imports make sense to me, as in the following example: def foobar(filename): if os.path.splitext(filename)[1] == '.gz': import gzip f = gzip.open(filename) else: f = file(filename) # etc. I

Re: PEP 8 exegetics: conditional imports?

2009-08-07 Thread Peter Otten
kj wrote: Conditional imports make sense to me, as in the following example: def foobar(filename): if os.path.splitext(filename)[1] == '.gz': import gzip f = gzip.open(filename) else: f = file(filename) # etc. And yet, quoth PEP 8: - Imports

Re: PEP 8 exegetics: conditional imports?

2009-08-07 Thread Christian Heimes
kj wrote: I seek the wisdom of the elders. Is there a consensus on the matter of conditional imports? Are they righteous? Or are they the way of the wicked? imports in functions are dangerous and may lead to dead locks if they are mixed with threads. An import should never start a thread

Re: PEP 8 exegetics: conditional imports?

2009-08-07 Thread alex23
Peter Otten __pete...@web.de wrote: This criterion is unlikely to be met for the examples you give above. time is a built-in module, and gzip a thin wrapper around zlib which is also built-in. This is something I was _utterly_ unaware of. Is there a list of what modules are built-in readily

Re: PEP 8 exegetics: conditional imports?

2009-08-07 Thread Dave Angel
Albert Hopkins wrote: On Fri, 2009-08-07 at 16:50 +, kj wrote: Conditional imports make sense to me, as in the following example: def foobar(filename): if os.path.splitext(filename)[1] == '.gz': import gzip f = gzip.open(filename) else: f = file(filename)

Re: PEP 8 exegetics: conditional imports?

2009-08-07 Thread Peter Otten
alex23 wrote: This is something I was _utterly_ unaware of. Is there a list of what modules are built-in readily available? sys.builtin_module_names ('__builtin__', '__main__', '_ast', '_bisect', '_codecs', '_collections', '_functools', '_locale', '_random', '_socket', '_sre', '_struct',