Changeset 24
- Timestamp:
- 26/06/09 10:00:45 (4 years ago)
- File:
-
- 1 edited
-
trunk/traducidos/modules.rst (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/traducidos/modules.rst
r22 r24 475 475 de módulos largos. 476 476 477 The only solution is for the package author to provide an explicit index of the 478 pa ckage. The import statement uses the following convention: if a package's479 :file:`__init__.py` code defines a list named ``__all__``, it is taken to be the480 list of module names that should be imported when ``from package import *`` is 481 encountered. It is up to the package author to keep this list up-to-date when a 482 new version of the package is released. Package authors may also decide not to 483 support it, if they don't see a use for importing \* from their package. For 484 example, the file :file:`sounds/effects/__init__.py` could contain the following 485 code::477 La única solución es que el autor del paquete provea un índice explícito del 478 paquete. La declaración ``import`` usa la siguiente convención: si el código 479 del :file:`__init__.py` de un paquete define una lista llamada ``__all__``, se 480 toma como la lista de los nombres de módulos que deberían ser importados cuando 481 se hace ``from package import *``. Es tarea del autor del paquete mantener 482 actualizada esta lista cuando se libera una nueva versión del paquete. Los 483 autores de paquetes podrían decidir no soportarlo, si no ven un uso para 484 importar \* en sus paquetes. Por ejemplo, el archivo 485 :file:`sounds/effects/__init__.py` podría contener el siguiente código:: 486 486 487 487 __all__ = ["echo", "surround", "reverse"] 488 488 489 This would mean that ``from sound.effects import *`` would import the three 490 named submodules of the :mod:`sound` package. 491 492 If ``__all__`` is not defined, the statement ``from sound.effects import *`` 493 does *not* import all submodules from the package :mod:`sound.effects` into the 494 current namespace; it only ensures that the package :mod:`sound.effects` has 495 been imported (possibly running any initialization code in :file:`__init__.py`) 496 and then imports whatever names are defined in the package. This includes any 497 names defined (and submodules explicitly loaded) by :file:`__init__.py`. It 498 also includes any submodules of the package that were explicitly loaded by 499 previous import statements. Consider this code:: 489 Esto significaría que ``from sound.effects import *`` importaría esos tres 490 submódulos del paquete :mod:`sound`. 491 492 493 Si no se define ``__all__``, la declaración ``from sound.effects import *`` 494 *no* importa todos los submódulos del paquete :mod:`sound.effects` al espacio 495 de nombres actual; sólo se asegura que se haya importado el paquete 496 :mod:`sound.effects` (posiblemente ejecutando algún código de inicialización 497 que haya en :file:`__init__.py`) y luego importa aquellos nombres que estén 498 definidos en el paquete. Esto incluye cualquier nombre definido (y submódulos 499 explícitamente cargados) por :file:`__init__.py`. También incluye cualquier 500 submódulo del paquete que pudiera haber sido explícitamente cargado por 501 declaraciones ``import`` previas. Considerá este código:: 500 502 501 503 import sound.effects.echo … … 503 505 from sound.effects import * 504 506 505 In this example, the echo and surround modules are imported in the current 506 n amespace because they are defined in the :mod:`sound.effects` package when the507 ``from...import`` statement is executed. (This also works when ``__all__`` is 508 define d.)507 En este ejemplo, los módulos *echo* y *surround* se importan en el espacio de 508 nombre actual porque están definidos en el paquete :mod:`sound.effects` cuando 509 se ejecuta la declaración ``from...import``. (Esto también funciona cuando se 510 define ``__all__``). 509 511 510 512 Note that in general the practice of importing ``*`` from a module or package is
Note: See TracChangeset
for help on using the changeset viewer.
![(please configure the [header_logo] section in trac.ini)](/python-tutorial/chrome/site/your_project_logo.png)