Skip to content Skip to sidebar Skip to footer

Python: What Is The Difference Between These Two Import Statements?

They both functionally looks same to me. Are there any differences and advantages of using one over another? >>> from datetime import datetime, timedelta >>> from

Solution 1:

If you wrap the imports in parens, you don't have to use a backslash for line continuation if you put a line break in the import statement, which is the preferred style. Functionally, they are identical, and if on one line, leaving out the parens is cleaner.

Solution 2:

Both of them are same:

In [17]: import dis

In [18]: deffunc1():
   ....:     from datetime import datetime, timedelta
   ....:     

In [19]: deffunc2():
   ....:     from datetime import (datetime, timedelta)
   ....:     

In [20]: dis.dis(func1)
  20 LOAD_CONST               1 (-1)
              3 LOAD_CONST               2 (('datetime', 'timedelta'))
              6 IMPORT_NAME              0 (datetime)
              9 IMPORT_FROM              0 (datetime)
             12 STORE_FAST               0 (datetime)
             15 IMPORT_FROM              1 (timedelta)
             18 STORE_FAST               1 (timedelta)
             21 POP_TOP             
             22 LOAD_CONST               0 (None)
             25 RETURN_VALUE        

In [21]: dis.dis(func2)
  20 LOAD_CONST               1 (-1)
              3 LOAD_CONST               2 (('datetime', 'timedelta'))
              6 IMPORT_NAME              0 (datetime)
              9 IMPORT_FROM              0 (datetime)
             12 STORE_FAST               0 (datetime)
             15 IMPORT_FROM              1 (timedelta)
             18 STORE_FAST               1 (timedelta)
             21 POP_TOP             
             22 LOAD_CONST               0 (None)
             25 RETURN_VALUE      

Solution 3:

There is no difference other than the first looks a little nicer to me.

As a side note, it appears that PEP 8 also uses the first form in an example although it doesn't say anything to explicitly exclude the second form from being preferred.

Solution 4:

An addition to @sr2222's answer. Generally, you only need these parentheses if you want to continue writing on the next line. For example, you can use the parentheses for declaring a string on two lines in of the two following ways:

In [1]: s1 ='abc' \
   ...:     'def'In [2]: s1
Out[2]: 'abcdef'In [3]: s2 = ('abc'
   ...:    'def')

In [4]: s2
Out[4]: 'abcdef'

The same goes for if-statements, for example. Use the parentheses to split the expression to multiple lines:

In[6]: if1in \
   ...:     [1,2,3]:
   ...:     passIn[7]: if (1 in
   ...:     [1,2,3]):
   ...:     pass

Both versions are equal in functionality. But using parentheses instead of the backslash is a better style. It's the same with your import statements. If the whole expression fits on one line, you don't need the parentheses at all.

Solution 5:

No, there isn't any difference. Commas and brackets are general Python syntax for tuple literals. You might see them anywhere. As you've discovered, there are two variations of the syntax, because the brackets are optional. Both alternatives return the same value:

>>>4,5
(4, 5)

>>>(4,5)
(4, 5)

However in some more complicated contexts, the parser understands commas differently, so it's necessary to use brackets if you really want a tuple. For instance f(3,4) is not equivalent to f((3,4)).

See the docs at http://docs.python.org/2/library/stdtypes.html#sequence-types-str-unicode-list-tuple-bytearray-buffer-xrange and http://docs.python.org/2/reference/grammar.html (hardcore)

Post a Comment for "Python: What Is The Difference Between These Two Import Statements?"