[Numpy-discussion] "import numpy" performance

Nathaniel Smith njs@pobox....
Mon Jul 2 16:59:41 CDT 2012


On Mon, Jul 2, 2012 at 10:06 PM, Robert Kern <robert.kern@gmail.com> wrote:
> On Mon, Jul 2, 2012 at 9:43 PM, Benjamin Root <ben.root@ou.edu> wrote:
>>
>> On Mon, Jul 2, 2012 at 4:34 PM, Nathaniel Smith <njs@pobox.com> wrote:
>
>>> I think this ship has sailed, but it'd be worth looking into lazy
>>> importing, where 'numpy.fft' isn't actually imported until someone
>>> starts using it. There are a bunch of libraries that do this, and one
>>> would have to fiddle to get compatibility with all the different
>>> python versions and make sure you're not killing performance (might
>>> have to be in C) but something along the lines of
>>>
>>> class _FFTModule(object):
>>>   def __getattribute__(self, name):
>>>     mod = importlib.import_module("numpy.fft")
>>>     _FFTModule.__getattribute__ = mod.__getattribute__
>>>     return getattr(mod, name)
>>> fft = _FFTModule()
>>
>> Not sure how this would impact projects like ipython that does
>> tab-completion support, but I know that that would drive me nuts in my basic
>> tab-completion setup I have for my regular python terminal.  Of course, in
>> the grand scheme of things, that really isn't all that important, I don't
>> think.
>
> We used to do it for scipy. It did interfere with tab completion. It
> did drive many people nuts.

Sounds like a bug in your old code, or else the REPLs have gotten
better? I just pasted the above code into both ipython and python
prompts, and typing 'fft.<tab>' worked fine in both cases. dir(fft)
works first try as well.

(If you try this, don't forget to 'import importlib' first, and note
importlib is 2.7+ only. Obviously importlib is not necessary but it
makes the minimal example less tedious.)

-n


More information about the NumPy-Discussion mailing list