Uses of Python's "from" keyword?

21,545

Solution 1

No and yes.

According to the official Python 2.7.2 grammar, the only occurrence of the word from is in the clause import_from, so no.

In the Python 3.1.3 grammar a new clause

raise_stmt: 'raise' [test ['from' test]]

appears, so yes.

Solution 2

There is a new syntax for delegating to a subgenerator in Python 3.3 which uses the from keyword.

Solution 3

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

def flatten(l):
    for element in l:
        if type(element) == type(list()):
            yield from flatten(element)
        else:
            yield element

def flatten2(l):
    for element in l:
        if type(element) == type(list()):
            yield flatten2(element)
        else:
            yield element


unflatted_list = [1,2,3,4,[5,6,[7,8],9],10]
flatted_list = list(flatten(unflatted_list))
flatted_list2 = list(flatten2(unflatted_list))
print(flatted_list) # [1,2,3,4,5,6,7,8,9,10]
print(flatted_list2) # [1, 2, 3, 4, <generator object flatten2 at 0x000001F1B4F9B468>, 10]

Solution 4

With the finalization of PEP 3134, the from keyword can be used when an exception is generated (raise) as a consequence of catching an exception in a try-except block.

try:
    <some code>
except <exception type> as e:
    raise <exception> from e

The keyword from allows to keep track of the caught exception e in the new excaption raised. The exception e will be stored in the attribute __cause__ of the new exception.

Solution 5

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.

Share:
21,545
Shon Freelen
Author by

Shon Freelen

Self taught Python and Java.

Updated on August 27, 2020

Comments

  • Shon Freelen
    Shon Freelen over 3 years

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

  • Karl Knechtel
    Karl Knechtel over 12 years
    Did you mean to post this as an answer for another question? It seems only tangentially related to this one...
  • Benjamin Peterson
    Benjamin Peterson over 12 years
    Actually, it does import from the __future__ module, too.
  • Adam Rosenfield
    Adam Rosenfield over 12 years
    @Karl: No, I did not. Yes, __future__ is a module, and importing from __future__ is an import statement. But my point is that while importing from future is syntactically the same as any other import, the semantics are different from a vanilla import. It's not just importing other symbol names, it's also changing the behavior of the interpreter in an important way.
  • agf
    agf over 12 years
    @Karl @Benj - The Python docs specifically call this a __future__ statement -- it looks like an import statement but it's not. If you actually want to import the __future__ module, you have to do import __future__.
  • dhanshreeA
    dhanshreeA over 3 years
    So in conclusion, from Python 3.3+ from can be used with raise and yield in addition to imports?
  • dhanshreeA
    dhanshreeA over 3 years
    the type(...) == list comparison here is redundant since yield from <expr> only works when <expr> is an Iterable.