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

import inside functions #117

Closed
wingedRuslan opened this issue Jun 4, 2019 · 6 comments
Closed

import inside functions #117

wingedRuslan opened this issue Jun 4, 2019 · 6 comments
Assignees

Comments

@wingedRuslan
Copy link
Collaborator

Hi!

There are a bunch of functions, where import statements are inside the functions' body.
Is this made intentionally? Should I refactor and include import only at the top of a module??

Some references:

  1. PEP8

Imports are always put at the top of the file, just after any module comments and docstrings, and before module globals and constants.

  1. StackOverFlow Answer

Module importing is quite fast, but not instant. This means that:

  • Putting the imports at the top of the module is fine, because it's a trivial cost that's only paid once.
  • Putting the imports within a function will cause calls to that function to take longer.
@Islast
Copy link
Collaborator

Islast commented Jun 5, 2019

Yes you should always refactor the imports to be at the top of the module

@Islast Islast self-assigned this Jun 10, 2019
@Islast
Copy link
Collaborator

Islast commented Jun 10, 2019

self assigning so that I remember to add this to the development guide

@wingedRuslan
Copy link
Collaborator Author

@Islast What are you planning to add to the guide?

@Islast
Copy link
Collaborator

Islast commented Jun 10, 2019

A note that in general imports should go at the top of the file

@Islast Islast mentioned this issue Jun 13, 2019
1 task
@KirstieJane
Copy link
Member

(This is almost certainly me having no idea what I was doing when I wrote this code! Thank you for doing a much cleaner job @wingedRuslan & @Islast 💖)

@Islast
Copy link
Collaborator

Islast commented Sep 20, 2019

Closing since #120 adds this style reccomendation to the documentation

@Islast Islast closed this as completed Sep 20, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants