Heim  >  Fragen und Antworten  >  Hauptteil

python - Welche Bedeutung haben die Klammern () bei der Verwendung von from XXX import (XXX, XXX, XXX,) zum Importieren eines Moduls?

Beim Lesen des Django-Quellcodes habe ich festgestellt, dass beim Importieren von Modulen immer obere Klammern hinzugefügt werden, zum Beispiel:

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,
)

Bitte erzählen Sie mir vom XXX-Import (XXX, XXX, XXX,) Welche Bedeutung haben die Klammern beim Import solcher Module?

世界只因有你世界只因有你2711 Tage vor1074

Antworte allen(2)Ich werde antworten

  • 仅有的幸福

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

    这是一种编码规范,始于PEP 328。在不加括号时,需要在换行的时候,行末要么加上反斜杠,如下所示:

    from xxx import aaa, bbb, \
        ccc

    要么每一行都写一遍from xxx import yyy

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

    有了括号就可以在括号内随意换行:

    from xxx import (
        aaa,
        bbb,
        ccc,
    )

    参见PEP328

    Antwort
    0
  • PHPz

    PHPz2017-05-18 10:51:54

    个人理解:

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

    等效

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

    语法看上去简洁些,一眼看上去就知道哪些方法是从一个模块中导入的。 也是习惯问题吧,仁者见仁智者见智。个人写着舒服就OK

    Antwort
    0
  • StornierenAntwort