1

I earlier researched lazy import of modules and found this way of doing it:

def some_funk():
    lazy_module = __import__("lazy_module")
    lazy_obj = lazy_module.LazyClass()
    lazy_obj.do_stuff()

Then I've seen some examples simply using:

def some_funk()
    import lazy_module
    lazy_obj = lazy_module.LazyClass()
    lazy_obj.do_stuff()

I prefer the later use and will rewrite my code to this.

But my question is if there is any difference between the two ways of doing lazy imports

Pablo
  • 377
  • 1
  • 14
user3532232
  • 257
  • 8
  • 19
  • 2
    Possible duplicate of [Difference between import and \_\_import\_\_ in Python](https://stackoverflow.com/questions/15401012/difference-between-import-and-import-in-python) – a_guest Oct 03 '19 at 10:06
  • 1
    Your first example should be `lazy_module = __import__("lazy_module")` without `.lazy_module`. – sanyassh Oct 03 '19 at 10:10
  • Link a_guest provided contains valid response for this question. – user5214530 Oct 03 '19 at 10:18

1 Answers1

3

You might want to check the documentation for import out. import lazy_module is internally calling __import__("lazy_module").

The lazy part of the import comes from both of them being done in a function, and not in the top of the class/script.

Pablo
  • 377
  • 1
  • 14