3 ms·
> Why not just `import mpmath.mp as mp` or something that has basically the same syntax for importing an object as for importing the module? Because if we appl
by paulluuk 3y ago
> Why not just `import mpmath.mp as mp` or something that has basically the same syntax for importing an object as for importing the module?
Because if we applied that as a "rule", then an import like this:
from keras.applications.vgg16 import VGG16, preprocess_input
Would become
import keras.applications.vgg16.VGG16 as VGG16
import keras.applications.vgg16.preprocess_input as preprocess_input
Which seems unnecessarily long.
Instead, I personally don't understand why everyone shortens numpy to np. I suspect it's because it's mostly used by people with a math background, who also believe that greek letters make for better variable names than actual spelled out variable names.
- TrianguloY 3y agoIt would become import keras.applications.vgg16.VGG16 import keras.applications.vgg16.preprocess_input Which is still long but more explicit (and python likes that). Imports cannot have dots so by default it should use the same name as the object itself, which is the word after the last dot. If still necessary to differentiate, the perfect alternative would be to flip it. import VGG16, preprocess_input from keras.applications.vgg16 In fact I've always find it strange that an import needs different syntax depending on how you use it. If all imports started with "import" it would be much more consistent and easy to read.
- dist-epoch 3y agoPython imports don't work like in Java. import keras.applications.vgg16.VGG16 import keras.applications.vgg16.preprocess_input preprocess_input(...) # ERROR keras.applications.vgg16.preprocess_input(...) # CORRECT
- Timon3 3y agoimport VGG16, preprocess_input from keras.applications.vgg16 This approach has a couple of downsides: - no editor autocomplete while typing (JS modules have the same downside) - more difficult to read multiple imports from modules in the same hierarchy (since the "from" parts aren't aligned) - import sorting becomes less clear (do you sort by imported things or by the module you import from?) JS uses your suggested format, and I dislike it much more than the Python equivalent due to the reasons stated above.
- deleted 3y ago[deleted]
- codexb 3y agoI agree, both those libraries just have bad api conventions leading to user errors. They've just become so pervasive now that bad habits have become the standard convention in those spaces.
- dkersten 3y agoIn Clojure we have (using :require in the namespace declaration): (:require [foo.bar]) (:require [foo.bar :as quux]) (:require [foo.bar :refer [x y z]]) …etc There’s no reason you couldn’t have similar syntax in Python.