Uses of Python's “from” keyword?

前端 未结 6 627
一整个雨季
一整个雨季 2021-01-11 11:53

Are there any other uses for Python\'s \"from\" keyword aside from import statements?

6条回答
  •  囚心锁ツ
    2021-01-11 12:29

    The following use

    from __future__ import some_feature
    

    is syntactically identical to an import statement but instead of importing a module, it changes the behavior of the interpreter in some fashion, depending on the value of some_feature.

    For example, from __future__ import with_statement allows you to use Python's with statement in Python 2.5, even though the with statement wasn't added to the language until Python 2.6. Because it changes the parsing of source files, any __future__ imports must appear at the beginning of a source file.

    See the __future__ statement documentation for more information.

    See the __future__ module documentation for a list of possible __future__ imports and the Python versions they are available in.

提交回复
热议问题