¿Usos de la palabra clave "desde" de Python?

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

preguntado el 28 de agosto de 11 a las 04:08

6 Respuestas

No y sí.

Según la official Python 2.7.2 grammar, the only occurrence of the word from is in the clause import_from, entonces no.

En los Python 3.1.3 grammar a new clause

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

appears, so yes.

Respondido 28 ago 11, 09:08

Hay un nuevo sintaxis para delegar a un subgenerador in Python 3.3 which uses the from palabra clave.

Respondido el 01 de Septiembre de 11 a las 12:09

So in conclusion, from Python 3.3+ from can be used with raise and yield in addition to imports? - alannaC

In Python 2.x, the only use of from es para el from x import y statement. However, for Python 3.x, it can be used in conjunction with the raise declaración, por ejemplo:

try:
    raise Exception("test")
except Exception as e:
    raise Exception("another exception") from e

Respondido 28 ago 11, 09:08

What does this do, exactly? Is the original exception implicitly chained somehow? That seems un-Pythonic to me; better to explicitly take it as a constructor parameter... so surely it does something else? - Karl Knechtel

Personally I've never used it -- I just know it exists, and it's intended to be for raising an exception that was directly caused by another. - user193476

Since there are a lot of updates to python from the time of posting the question, here is a new use case of desde 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]

Respondido 19 ago 18, 21:08

los type(...) == list comparison here is redundant since yield from <expr> solo funciona cuando <expr> is an Iterable. - alannaC

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.

Por ejemplo, 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.

Consulta los __future__ statement documentation para más información.

Consulta los __future__ documentación del módulo for a list of possible __future__ imports and the Python versions they are available in.

Respondido el 08 de Septiembre de 11 a las 08:09

Did you mean to post this as an answer for another question? It seems only tangentially related to this one... - Karl Knechtel

Actually, it does import from the __future__ module, too. - Benjamin Peterson

@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 semántica 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. - Adam Rosenfield

@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__. - agf

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

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

La palabra clave 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.

Respondido 26 ago 20, 15:08

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.