0

I'm writing some views in Django, the are just python functions really. I'm curious as to whether there's a better way for me to arrange my files.

It this...

import a, b
def x(request): return a(request)
def y(request): return b(request)

Less efficient than putting it in two files?

import a
def x(request): return a(request)

and

import b
def y(request): return b(request)

Since for each request made only one of these functions will be called, it seems to me that having the other one in the same file and importing all the modules the other one needs is a bad idea. Am I right? Does django just import the whole lot anyway?

Jake
  • 12,713
  • 18
  • 66
  • 96
  • 1
    "Am I right?" Did you measure a specific performance problem? If so, please post the details of the performance problem you measured. Your Django modules are a loaded once when you start up, then never again, so it's hard to measure the performance of imports. But if you've got a specific problem, it helps us to see the details. – S.Lott Nov 11 '10 at 21:21
  • Semi-related: http://stackoverflow.com/questions/128478/should-python-import-statements-always-be-at-the-top-of-a-module – bboe Nov 11 '10 at 21:26
  • Thanks @bboe. @S.Lott, no specific problem, just wanting to know the ins and outs so as to avoid problems coming up. – Jake Nov 11 '10 at 23:49
  • No problem can come up. Your Django modules are a loaded once when you start up, then never again. There is no potential problem. Please focus on actual problems. Please. – S.Lott Nov 12 '10 at 02:31

4 Answers4

3

Unless you're running Django via CGI (which I really hope you're not), the imports will be cached after the first time they're performed, and this whole argument is meaningless.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • It's not really an argument, I guess what I really needed to know was that the imports are cached and that the caching lasts across requests (under mod_WSGI), not like PHP which starts with a clean slate each request. – Jake Nov 11 '10 at 23:52
2

There's not a whole lot of difference, use whatever is most readable.

dan_waterworth
  • 6,261
  • 1
  • 30
  • 41
0

You don't need to worry about how many files you use; the difference in performance is negligible. That said, there are more efficient and readable ways to use imports.

Only import what you need; never use from x import * unless you are really going to use everything in X. Don't nest imports; instead of import a, b, write:

import a
import b

As for your specific issue, you should have a module to represent a typical course of action. If you always have to run two functions, one after the other, put them in the same file. If not, do whatever makes the most sense to you and the people that you're working with. Remember, imports that take the form import a (as opposed to from x import y, z) are relatively cheap.

Rafe Kettler
  • 75,757
  • 21
  • 156
  • 151
  • Does `import a, b` really make any difference versus `import a; import b`? Or are you just pulling that out of your ass. – Falmarri Nov 11 '10 at 23:42
  • @Falmarri it's not a performance difference, it's a readability difference. Nested imports are considered terrible style – Rafe Kettler Nov 11 '10 at 23:43
  • Thanks, I wasn't aware that imports using `from` are less efficient, but that makes sense. – Jake Nov 11 '10 at 23:46
0

Also, instead of doing this:

def x(request): return a(request)

it would probably be better to call the original function.

Eric Pauley
  • 1,709
  • 1
  • 20
  • 30