Why import statements with parentheses?

LarsVegas picture LarsVegas · Apr 9, 2015 · Viewed 8.5k times · Source

Lately have seen imports like this

from module import (function, another_function, 
                    another_function)

Seemingly this has been done to be able to stretch the import statement over more than one line. In cases like this I usually just import like so

from module import function, another_function, \
            another_function

What exactly are the parentheses doing in this case and are they considered to be bad practice?

Answer

deceze picture deceze · Apr 9, 2015

As PEP 8 states:

The preferred way of wrapping long lines is by using Python's implied line continuation inside parentheses, brackets and braces. Long lines can be broken over multiple lines by wrapping expressions in parentheses. These should be used in preference to using a backslash for line continuation.