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

好久不见. 提交于 2019-12-04 03:43:34

问题


They both functionally looks same to me. Are there any differences and advantages of using one over another?

>>> from datetime import datetime, timedelta
>>> from datetime import (datetime, timedelta)

回答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.




回答2:


Both of them are 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      



回答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.




回答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]: if 1 in \
   ...:     [1,2,3]:
   ...:     pass

In [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.




回答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)



来源:https://stackoverflow.com/questions/14284141/python-what-is-the-difference-between-these-two-import-statements

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!