Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PEP 690: Add a simple example of the behavior #2577

Merged
merged 1 commit into from
May 7, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions pep-0690.rst
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,41 @@ Dynamic imports using ``__import__()`` or ``importlib.import_module()`` are
also never lazy.


Example
-------

Say we have a module ``spam.py``::

# simulate some work
import time
time.sleep(10)
print("spam loaded")

And a module ``eggs.py`` which imports it::

import spam
print("imports done")

If we run ``python -L eggs.py``, the ``spam`` module will never be imported
(because it is never referenced after the import), ``"spam loaded"`` will never
be printed, and there will be no 10 second delay.

But if ``eggs.py`` simply references the name ``spam`` after importing it, that
will be enough to trigger the import of ``spam.py``::

import spam
print("imports done")
spam

Now if we run ``python eggs.py``, we will see the output ``"imports done"``
printed first, then a 10 second delay, and then ``"spam loaded"`` printed after
that.

Of course, in real use cases (especially with lazy imports), it's not
recommended to rely on import side effects like this to trigger real work. This
example is just to clarify the behavior of lazy imports.


Debuggability
-------------

Expand Down