Thanks to Tom / Matias / Brian for taking the time to reply, and for
providing some good links, this was *exactly* what I needed.

In regards to circular import issues, this is pretty much the main reason
I've put imports inside function/method before, and I agree that most of the
time it can be resolved with re-organization.

Pretty shameful that after almost 6 years of (fairly advanced) Python
coding, I still finding myself not knowing some of the basic 101 stuff..
Slightly off topic but, does anyone else have that issue when coding? (i.e.
doing really complex code, but forgetting/not knowing some of the real basic
stuff).

On Thu, Jun 2, 2011 at 3:56 PM, Brian Bouterse <bmbou...@gmail.com> wrote:

> Yes relative imports are explicitly discouraged in PEP 
> 8<http://www.python.org/dev/peps/pep-0008/>.
>  Look for the imports section.
>
> I do not recommend importing at runtime which is what you are doing when
> you put imports inside function/methods.  There are performance problems
> when calling imports repeatedly.  One possible method is to use a lazy
> import paradigm described at the end of this section on import 
> overhead<http://wiki.python.org/moin/PythonSpeed/PerformanceTips#Import_Statement_Overhead>
> .
>
> The only reason I think it is ok to do imports inside function/method code
> is if you don't know what you need to import until runtime, but in those
> cases you wouldn't use the standard "import foo" syntax, since you would
> need to dynamically choose what to import.
>
> Another reason folks hide imports inside function/method code is to solve
> circular import issues.  This is a consistently unproductive solution and
> will create problems over time through hidden import dependencies that
> aren't discovered until runtime.  circular imports can almost always be
> resolved through some code and package reorganization.  I like this
> article <http://effbot.org/zone/import-confusion.htm> on how import works
> in python.
>
> Brian
>
>
>
> On Thu, Jun 2, 2011 at 10:39 AM, 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)
>>
>> Obviously, the answers would depend on different scenarios, so I'm looking
>> for a discussion really, rather than a set in stone answer.
>>
>> Example 1:
>> import os
>> def test():
>>    print os.uname()
>> def test2():
>>    print os.uname()
>> test()
>> test2()
>>
>> Example 2:
>> import os
>> class test:
>>    def test(self):
>>        print os.uname()
>>
>>
>> Example 2:
>> class test:
>>    def test(self):
>>        import os
>>        print os.uname()
>>    def test2(self):
>>        import os
>>        print os.uname()
>>
>> t = test()
>> t.test()
>> t.test2()
>>
>>  --
>> 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.
>>
>
>
>
> --
> Brian Bouterse
> ITng Services
>
> --
> 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.
>

-- 
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.

Reply via email to