Le 16/02/2011 02:05, Toshio Kuratomi a écrit :
> On Mon, Feb 14, 2011 at 05:27:44PM +0100, Arve Knudsen wrote:
>> Does it matter in practice how many times a module gets imported locally to a
>> function though? I find that in certain cases I have to defer importing of
>> certain modules until they are used, so it's not just a matter of style. I
>> don't think pylint should by default warn about local imports, that's just
>> way
>> too strict unless there's some good reason I'm unaware of.
>>
> Someone else already posted the stylistic reasons not to do this (and for
> those alone, I think pylint is right to have a message for this). There's
> also an efficiency reason. If you have a function that gets called
> repeatedly and it's doing a reimport, the import machinery does cause a slow
> down (even though python discovers that the module has already been imported
> and skips some of the steps.)
>
> I recently had to optimize a piece of code where I was doing a local import
> because the import had a circular dependency. I found the circular imports
> section of this page:
> http://effbot.org/zone/import-confusion.htm
>
> to be helpful in showing me how to get around this issue without a local
> import.
>
> -Toshio
>
A classical reason why I do local import is when a module can be called
either as main for test purpose or as an import in a larger software...
In this case I often don't want all imports to be global.
--
Pierre
_______________________________________________
Python-Projects mailing list
[email protected]
http://lists.logilab.org/mailman/listinfo/python-projects