7timesTom wrote:
> I have a large number of view functions which require various python,
> django and other import statements.
>
> My question is: is it ok to put ALL my import statements at the top of
> views.py and then not have to worry about which view fuction uses
> what. Or must I put my imports within the view fuctions that require
> them?
>
>   
It's OK, and indeed that's the normal way of programming.
> Does it depend on how many functions require I certain import? And if
> so, how many imports justifies a global import at the top of the code?
>
>   
One.
> I'm interested on the effect this decision has on speed/memory usage
> etc.
>   
If you want to, you can do some timings with the "timeit" Python module,
but essentially the usual justifications for putting imports inside
functions are

a. The function is very rarely called: putting the import inside the
function means that the import may never have to be executed.

b. Lowering startup overhead: with the import inside the function it
needn't be performed until the function is called, so the program gets
started quicker.

Almost no impact on speed unless you are using a heavily-layered
approach to your programming with many function calls. The import
functionality checks the sys.modules dict first thing to see whether the
module has already been imported. If so then the value of the
sys.modules entry is immediately used to satisfy the import, so there
isn't much overhead.

regards
 Steve


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to