3 ms·
For me, Python's "from library import module as myname" is unbeatable. I like to choose my own namespaces and assign whatever bits of a lib to it as I feel like
by nonrandomstring 4y ago
For me, Python's "from library import module as myname" is unbeatable.
I like to choose my own namespaces and assign whatever bits of a lib
to it as I feel like. It seems robust and flexible.
- smcl 4y agoImporting a namespace under an alias exists in a few languages
- nerdponx 4y agoThis usage is so idiomatic and ubiquitous that you can see "np." or "pd." or "plt." in just about any code and assume that the user is using Numpy or Pandas or Matplotlib. Now that I think about it, I really should set up a "snippet" to type this for me: import matplotlib.pyplot as plt import numpy as np import pandas as pd
- soulofmischief 4y agoI prefer JavaScript's idiomatic approach: import anyNamespace from 'module.js' import { subModule } from 'module.js' import { subModule as anyName } from 'module.js'
- rtpg 4y agothe biggest problem with JS's is extremely incidental, but a total mess in practice. If you type from left to right it's impossible for auto-complete to help you with member names (since it can't know what module you're looking for!)
- soulofmischief 4y agoI work around this in two ways: A) I type "import {} from 'module" first and then go back and add modules or more often these days, B) I let Copilot guess for me, which combined with my predictable file organization usually results in the right autocomplete. Option A can be improved upon by writing a macro/template with slots in the correct order.
- nequo 4y agoPython really got that right. In Haskell, it’s similar: import qualified Foo.Bar as Baz
- tmtvl 4y agoDoes Python allow import symbol renaming? In R6RS Scheme I can do the following: (import (prefix (rename #;from (rnrs r5rs) (delay #;as eval-later) (force #;as eval-now)) r5rs:)) And from (rnrs r5rs) it will import delay as r5rs:eval-later and force as r5rs:eval-now.