On Thu, Jun 2, 2011 at 3:39 PM, Cal Leeming [Simplicity Media Ltd] <cal.leem...@simplicitymedialtd.co.uk> wrote: > Hey guys, > > This is more of a python question, than a Django specific one, but it's been > bugging me for years now lol. > > I'm trying to figure out if it is better to do imports at the top of the > file, or within the function/methods themselves. > I guess the questions I'm asking are: > > Is there any (serious) performance issues by doing imports every time the > function/method is called > Is there a preferred style / guideline when it comes to imports? (maybe a > PEP somewhere?) > Are relative imports discouraged? (from .. import package) >
To quickly answer your Qs: Yes, there is a slight cost in importing modules. Putting the import in the function adds an infinitesimal cost to the function. Imports are discussed in PEP 8 and relative imports in PEP 328. Relative imports are strongly discouraged. Cheers Tom -- 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 django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.