Unless I'm mistaken, these two lines do exactly the same thing:
import theano.tensor as T
from theano import tensor as T
Is there any reason why there are redundant ways to import in Python?
So there's four different ways to import: Import the whole module using its original name: pycon import random. Import specific things from the module: pycon from random import choice, randint. Import the whole module and rename it, usually using a shorter variable name: pycon import pandas as pd.
Redundancy can be avoided using Master Data. Master data is a single source of data accessed by several applications and systems.
Using import * in python programs is considered a bad habit because this way you are polluting your namespace, the import * statement imports all the functions and classes into your own namespace, which may clash with the functions you define or functions of other libraries that you import.
The statement
import theano.tensor
only works if theano.tensor
is a module itself. This is what permits both your examples to work.
Consider sys.argv
, which is not a module. The following works:
from sys import argv
which imports argv
into the global namespace, but the import
form does not:
>>> import sys.argv
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named argv
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With