Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should import be inside or outside a Python class?

Tags:

python

Overview

Suppose I'm building a class for general usage: I might need to import it wherever, use it in a couple other files, etc. Should the import go before the class, as:

import foo

class Bar():

    def __init__(self):
        foo.spam()

Or inside the __init__ method, as:

class Bar():

    def __init__(self):

        import foo

        foo.spam()

My Analysis

Outside

+ Brings the foo into the global namespace for use throughout Bar

- Importing Bar also requires you to manually import foo (@MartijnPieters et al.)

Inside

+ Avoids loading foo before you actually need to

+ foo loads when Bar is instantiated imported (and used) (@BrenBarn)

- foo is unavailable elsewhere

like image 273
user126350 Avatar asked Sep 03 '14 17:09

user126350


People also ask

Where should I put import in Python?

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

Can you import into a class Python?

Python modules can get access to code from another module by importing the file/function using import. The import statement is that the commonest way of invoking the import machinery, but it's not the sole way. The import statement consists of the import keyword alongside the name of the module.

Should you import inside function Python?

Importing inside a function will effectively import the module once.. the first time the function is run. It ought to import just as fast whether you import it at the top, or when the function is run. This isn't generally a good reason to import in a def.

What is correct way to import a Python module?

Importing Modules To make use of the functions in a module, you'll need to import the module with an import statement. An import statement is made up of the import keyword along with the name of the module. In a Python file, this will be declared at the top of the code, under any shebang lines or general comments.


2 Answers

Usually all imports go at the top of the module. That makes it easy to see the dependencies of a module at a glance, either by visual inspection or in a code checker like pyflakes. Your assumption that "importing Bar also requires you to manually import foo" is false.

The only times you would imports inside functions or methods are when the imports are very resource-intenstive (e.g., slow) or unreliable (e.g., optional dependencies that may not be installed, platform-specific modules or modules that tend to break), and client code is not expected to always call the functions in question.

like image 129
Fred Foo Avatar answered Oct 18 '22 22:10

Fred Foo


There are some related questions here and here, among others. The bottom line is that it's usually best to put all the imports at the top unless you have a good and specific reason not to do so.

As for your analysis:

Importing Bar also requires you to manually import foo

No. Importing Bar will automatically import foo, and Bar will be able to use foo regardless of what any other code does. Imports create a reference to the imported module only inside the module doing the import (that is, they don't make the imported name "globally available" across all modules).

  • foo loads when Bar is imported (and used)

Note that "imported" and "used" aren't the same thing. In your example foo will be imported when Bar is instantiated (with Bar()), because it is inside the __init__ method. It won't be loaded when Bar is imported.

  • Avoids loading foo before you actually need to

This is true, but in practice it usually gains you little. It only makes sense to worry about this if there is a reasonably high chance that someone using your module will not ever need to use the part that imports foo (i.e., Bar). Even then, it only makes sense if importing foo is slow, resource-intensive, or sometimes may not work (e.g., if foo is a library that may not be available on all platforms). For most modules, you gain little by deferring the import.

Which brings up one big reason not to do the local import: it makes it harder to know what the dependencies of a module are. It's handy to be able to look at the top of a module and see everything it needs to import. If crucial imports are hidden deep inside code, it's more difficult to understand what other modules need to be available.

like image 3
BrenBarn Avatar answered Oct 18 '22 21:10

BrenBarn