Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are there redundant ways to import in Python?

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?

like image 698
tumultous_rooster Avatar asked May 20 '15 20:05

tumultous_rooster


People also ask

What are the different 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.

What can be used to avoid redundancy in Python?

Redundancy can be avoided using Master Data. Master data is a single source of data accessed by several applications and systems.

Why is the use of import all statements not recommended in Python?

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.


1 Answers

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
like image 125
Greg Hewgill Avatar answered Oct 12 '22 17:10

Greg Hewgill