Skip to content

Instantly share code, notes, and snippets.

@hholst80
Created May 15, 2023 20:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hholst80/f59e15334bc5cca720a3a44241bb8eaa to your computer and use it in GitHub Desktop.
Save hholst80/f59e15334bc5cca720a3a44241bb8eaa to your computer and use it in GitHub Desktop.
Cargo cult GTP

why should imports be top level and not in the function body where the functions are used?

ChatGTP 4

There are several reasons why it is typically recommended to place import statements at the top of your Python files:

  1. Readability and maintainability: Placing all imports at the top of the file makes it easy to see all of the file's dependencies in one place. This makes it easier to understand the context in which the code is running.

  2. Performance: Importing a module has some overhead. If you import it inside a function that's called many times, you'll pay that overhead each time the function is called. In contrast, if you import the module at the top level, the import happens just once, no matter how many times functions in the module are called. Note that Python is smart enough not to reload the entire module each time it's imported, but there's still some overhead to checking whether the module needs to be updated.

  3. Avoiding circular imports: If you import modules at the top level, Python can more effectively prevent issues with circular imports, where module A imports module B and module B imports module A.

  4. Coding standards and conventions: The PEP 8 style guide, which is widely followed in the Python community, recommends that all imports be at the top of the file.

That said, there are some cases where it can make sense to import inside a function or a method:

  1. Conditional imports: If a module is only needed in some specific conditions, you might choose to import it only in the part of your code that handles those conditions.

  2. Reducing startup time: If a module is large and not always needed, importing it at the top level could slow down the startup time of your program. In such cases, it might make sense to import the module only when it's actually needed.

However, these should be the exceptions rather than the rule. In general, it's best to stick to the convention of importing at the top level.

@hholst80
Copy link
Author

What is the actual overhead for this?

Let's try it out.

z=0

def f1():
    import math
    global z
    x=123
    y=x*x
    z=x+y

def f2():
    global z
    x=123
    y=x*x
    z=x+y

import timeit
t1=timeit.timeit(f1)
print(z)
t2=timeit.timeit(f2)
print(z)
overhead=t1-t2
print('overhead micro seconds:',overhead)
$ python3 test.py
15252
15252
overhead micro seconds: 0.08821819299191702
$ python3 --version
Python 3.10.6

@hholst80
Copy link
Author

hholst80 commented May 15, 2023

So the overhead is 0.1 µs for importing a reasonably big thing like math.

If you include something specifically then? Say from math import cos

overhead micro seconds: 0.4897218330006581

that was unexpected. slower?

@hholst80
Copy link
Author

So in the end, do we care or not about a micro second? If the whole call takes 1 ms to execute that is 0.1% of the execution time. Wasted. Perhaps something to consider. Perhaps not.

@hholst80
Copy link
Author

Nim does not support function level imports, so if Nim is the future, I guess we'll have to buy into the cult.

It does create some clutter as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment