Python: What is the difference between these two import statements?

Both of them functionally look the same to me. Are there differences and advantages of using one over the other?

>>> from datetime import datetime, timedelta >>> from datetime import (datetime, timedelta) 
+4
source share
5 answers

If you transfer the import to parens, you do not need to use a backslash to continue the line if you put a line break in the import statement, which is the preferred style. They are functionally identical, and if on the same line, leaving partners, it becomes cleaner.

+9
source

Both are the same:

 In [17]: import dis In [18]: def func1(): ....: from datetime import datetime, timedelta ....: In [19]: def func2(): ....: from datetime import (datetime, timedelta) ....: In [20]: dis.dis(func1) 2 0 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) 2 0 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 
+7
source

there is no difference except the first, it looks a little nicer for me.

As a side note, it appears that PEP 8 also uses the first form in the example, although it does not say anything explicitly to exclude the second form is preferred.

+5
source

Addendum to @ sr2222's answer. Typically, you only need these parentheses if you want to continue recording on the next line. For example, you can use parentheses to declare a line on two lines in two 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 applies to if-statement, for example. Use parentheses to split the expression into multiple lines:

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

Both versions are the same in functionality. But using parentheses instead of backslash is the best style. This is the same with your import operations. If the whole expression matches one line, you don't need parentheses at all.

+2
source

No, there is no difference . Commas and parentheses are the general Python syntax for tuples. You can see them anywhere. As you discovered, there are two syntax options since brackets are optional. Both alternatives return the same value:

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

However, in some more complex contexts, the analyzer understands commas differently, so you need to use parentheses if you really want a tuple. For example, f(3,4) 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)

0
source

All Articles