Friday Finking: Imports, Namespaces, Poisoning, Readability

Tim Chase python.list at tim.thechases.com
Thu Jun 4 21:02:13 EDT 2020


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

For most of what I do, I only ever have one such module so I'm not
trying keep multiple short-names in my head concurrently.  For me,
it's usually tkinter which I import as either "tk" or just "t".  It's
usually cases where the whole content would dump a lot of junk in my
module's namespace (like tkinter or pygame), so I like to keep the
namespacing; but I also don't want to clutter my code with long
references to it.

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

Not really.  I'm a vi/vim/ed guy.  While vim has some nice "go to
the definition of the thing under the cursor" (gd/gD/tags/etc),
regardless of my $EDITOR, it's not overly taxing to see "tk.thing"
and if I had any question about it, jump to the top of the file where
all the import-statements are, and see that tkinter is being imported
as tk.  It's there explicitly, unlike those abominable "from tkinter
import *" tutorials.

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

I've not given it much thought, but I tend to use the above method
with PSL/3rd-party libraries where there's a lot of things in that
namespace; but for my own code, they're usually pretty svelte, so I
stick to "from mymodule import a, b, c" if there are lots or just an
"import mymodule" without aliasing it shorter.  If the imported names
were to grow to an unweildy size, I'd go the same route as above
("import myutils as u").

My largely irrelevant $0.02 USD, adjusted for tax and inflation,

-tkc








More information about the Python-list mailing list