Is it good practice to perform Imports within methods?

Asked

Viewed 114 times

5

Is there any advantage to doing import within methods instead of performing the "traditional" import in the program header?

If I import within the method, will the module only be imported when the method is called? Thus, the module is reimported every time the method is called?

I would like to know in which scenario using this flexibility of language is useful.

1 answer

8


Let’s go in pieces. The style guide used by most people, the document called PEP-8, recommends that imports be in the module header.

BUT you have to keep in mind that they are style recommendations and not iron and fire - the most important thing is to understand what happens, and then you check what’s best at each point of your project.

the module will only be imported when the method is called?

If the same module is not imported in any other module or place, yes, it will only be imported when the method is first called.

Thus, the module is reimported every time the method is called?

No! After a module is imported into Python, it is kept in an internal record (the dictionary sys.modules), and new import directives for the same module only create new variables, in the local context, pointing to the module that has already been read. That is: in subsequent calls to the same method the import "cost" is the same cost of creating a new variable, ie almost nothing.

Best practice is up to you to decide. Because of the style guide I put almost all imports in the header of the modules. But not always. It can be a fairly large package to import that will not be required in every exception of the program, for example: a function that exports a graph and for that matter matplotlib. Moving the import into a method in this case would make the start of the program faster, with the counterpart of a delay (always less than 1s in general) when the functionality is used the first time.

As a rule, if your program is a service that works as a server, it is better to import everything at the beginning, ensuring a minimum waiting time on all requests. If it is an interactive program, it may be interesting to choose to get a faster start and go making some imports afterwards.

In some projects where the test files get too large, I also choose to import the functions/classes being tested directly within the test functions, just to make more visible what I am testing. (But a reorganization could simply break everything into smaller files, for example).

And finally, sometimes, because of the very workings of your system, it’s necessary a late import of some modules. This is the case of applications in Flask, for example - where it is convenient that module __init__ have references to the views, but on the other hand, the view files need to have a reference to the object app defined in its own __init__. In general, this is solved by importing the views into the __init__ closer to the end of the file, but could be within a function as well.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.