Friday Finking: Imports, Namespaces, Poisoning, Readability

Rhodri James rhodri at kynesim.co.uk
Fri Jun 5 07:36:50 EDT 2020


AFAIKT the "three" are readability, naming conflicts and source location.

On 05/06/2020 01:15, DL Neil via Python-list wrote:
> - how do you like to balance these three (and any other criteria)?

Readability is king.  Or queen, if you prefer.  Anything that damages 
readability drops dramatically in its desirability.  One consequence of 
that is that I rarely use "import ... as", since as you mentioned, 
renamed libraries are a rich source of WTF moments.  I would use "import 
numpy as np" (if I ever used numpy) because it seems to be standard, but 
I wouldn't type "import pygame as pg" despite having used PyGame 
extensively in the past.

I tend to use "from xxx import yy_yy, zzzz" when I'm not importing many 
things from a library and their names are sufficiently obvious.  The 
definitions of "not many" and "sufficiently obvious" are pretty 
flexible.  In particular, I'll list more items as the qualified names 
get longer, as you noted.  Source location is a non-issue; it's trivial 
to jump to the imports at the top of the file, look it up and jump back 
again.  Or have two buffers viewing the same file; that's a technique I 
use quite a lot anyway.  If I need to know the signature, that's what 
documentation is for.

I would never rename an object I've imported using "as".  It's just not 
worth the inevitable heartache.  If that means I have namespace 
collisions, that's either a vote to use the qualified name or to change 
the name of whatever I wrote that clashes with it.

> - is your preference (or selection) influenced by the facilities offered 
> by your favorite editor/IDE?

Not really.  I'm an EMACS user, so any of the "fancy" IDE handiwork is 
immediately out.  The only reason you need an IDE is if your support 
tools suck (sorry, Windows users).

> - does your decision differ according to whether the 'target module' is 
> one of yours, from the PSL, from some third party, corporate, ...?

I'm more likely to change my own module to fit :-)

> - do you prefer PSL's importlib over Python's native import-s, for one 
> of these (or any other) reason?

Um, why would I?

-- 
Rhodri James *-* Kynesim Ltd


More information about the Python-list mailing list