209
209
> factory, then yes, foo_factory is what I would use.
215
To make startup time faster, we use the ``bzrlib.lazy_import`` module to
216
delay importing modules until they are actually used. ``lazy_import`` uses
217
the same syntax as regular python imports. So to import a few modules in a
220
from bzrlib.lazy_import import lazy_import
221
lazy_import(globals(), """
232
import bzrlib.transport
236
At this point, all of these exist as a ``ImportReplacer`` object, ready to
237
be imported once a member is accessed.
240
Modules versus Members
241
~~~~~~~~~~~~~~~~~~~~~~
243
While it is possible for ``lazy_import()`` to import members of a module
244
wehn using the ``from module import member`` syntax, it is recommended to
245
only use that syntax to load sub modules ``from module import submodule``.
246
This is because variables and classes can frequently be used without
247
needing a sub-member for example::
249
lazy_import(globals(), """
250
from module import MyClass
254
return isinstance(x, MyClass)
256
This will incorrectly fail, because ``MyClass`` is a ``ImportReplacer``
257
object, rather than the real class.
260
Passing to other variables
261
~~~~~~~~~~~~~~~~~~~~~~~~~~
263
It also is bad form to pass ``ImportReplacer`` objects to other variables.
264
Because the replacer only knows about the original name, it is unable to
265
replace other variables. The ``ImportReplacer`` class will raise an
266
exception if it can figure out that this happened. But it is not always
267
possible to know, so bugs can be secretly waiting to be exposed.