search

Home  >  Q&A  >  body text

python - When using from XXX import (XXX, XXX, XXX,) to import a module, what is the significance of the brackets ()?

When reading the django source code, I found that upper brackets are always added when importing modules, for example:

from django.core.exceptions import (
    DisallowedHost, ImproperlyConfigured, RequestDataTooBig,
)

from django.utils.encoding import (
    escape_uri_path, force_bytes, force_str, force_text, iri_to_uri,
)

Please tell me from XXX import (XXX, XXX, XXX,) What is the meaning of the brackets when importing the module like this?

世界只因有你世界只因有你2782 days ago1158

reply all(2)I'll reply

  • 仅有的幸福

    仅有的幸福2017-05-18 10:51:54

    This is a coding specification started with PEP 328. When not adding parentheses, you need to add a backslash at the end of the line when breaking the line, as shown below:

    from xxx import aaa, bbb, \
        ccc

    Or write each line againfrom xxx import yyy:

    from xxx import aaa
    from xxx import bbb
    from xxx import ccc

    With parentheses, you can wrap new lines at will inside the parentheses:

    from xxx import (
        aaa,
        bbb,
        ccc,
    )

    See PEP328

    reply
    0
  • PHPz

    PHPz2017-05-18 10:51:54

    Personal understanding:

    from django.core.exceptions import (
    DisallowedHost, ImproperlyConfigured, RequestDataTooBig
    )

    Equivalent

    from django.core.exceptions import DisallowedHost;
    from django.core.exceptions import ImproperlyConfigured;
    from django.core.exceptions import RequestDataTooBig;

    The syntax looks simpler, and you can tell at a glance which methods are imported from a module. It's also a matter of habit. The benevolent sees benevolence and the wise see wisdom. Personally, it’s OK if it’s comfortable

    reply
    0
  • Cancelreply