0
import theano.tensor as T
from theano import tensor as T 

Is there a difference between the two or are they same? Which is better?

HMK
  • 578
  • 2
  • 9
  • 24
  • 1
    One difference that I can think of is that if `tensor` is not a module, the first one won't work, while the second one will work whether tensor is a module or a method...AFAIK – elethan Oct 26 '16 at 18:52
  • Although the duplicated question isn't exactly the same level, the longest answer's explanation (from user *sapam*) covers the differences in this case. – Prune Oct 26 '16 at 18:54
  • I understand the difference between import module and from module import *. My question is about the "as" part. – HMK Oct 26 '16 at 19:06
  • `as` is just defining an alias for whatever is being imported. If you are importing the same thing, the affect of `as` will be the same in both cases. – elethan Oct 26 '16 at 19:08

2 Answers2

0

No, there's not. You're importing the same thing in both cases.

To read up on the differences between import module and from module import foo see this question

Community
  • 1
  • 1
Thaufeki
  • 177
  • 11
0

Although the SO posts linked in comments will probably give you the answer you are looking for, one thing I would like to add to the conversation is that the from import a.b will only work if b is a submodule of a, but will raise an error if b is not a module.

On the other hand, from a import b will work even if b is not a module (i.e., if it is a method of a). Observe:

In [1]: import os.path

In [2]: from os import path

In [3]: import datetime.datetime
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-3-8466c53a2255> in <module>()
----> 1 import datetime.datetime

ImportError: No module named datetime

In [4]: from datetime import datetime

I think from a import b is the preferred usage compared to import a.b - at least it is the one that I have come across far more often. The as ... part will operate the same in both cases.

elethan
  • 16,408
  • 8
  • 64
  • 87