Are there any other uses for Python's "from" keyword aside from import
statements?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Django __str__ returned non-string (type NoneType)
- Evil ctypes hack in python
In Python 2.x, the only use of
from
is for thefrom x import y
statement. However, for Python 3.x, it can be used in conjunction with theraise
statement, e.g.:There is a new syntax for delegating to a subgenerator in Python 3.3 which uses the
from
keyword.The following use
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'swith
statement in Python 2.5, even though thewith
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.No and yes.
According to the official Python 2.7.2 grammar, the only occurrence of the word
from
is in the clauseimport_from
, so no.In the Python 3.1.3 grammar a new clause
appears, so yes.
Since there are a lot of updates to python from the time of posting the question, here is a new use case of from keyword in python3 will show you the use with an example