Proposal: Inline Import

bonono at gmail.com bonono at gmail.com
Sun Dec 11 03:51:47 EST 2005


Shane Hathaway wrote:
> Mike Meyer wrote:
> > Shane Hathaway <shane at hathawaymix.org> writes:
> >>I'd like a way to import modules at the point where I need the
> >>functionality, rather than remember to import ahead of time.  This
> >>might eliminate a step in my coding process.  Currently, my process is
> >>I change code and later scan my changes to make matching changes to
> >>the import statements.   The scan step is error prone and time
> >>consuming. By importing inline, I'd be able to change code without the
> >>extra scan step.
> >
> >
> > As others have pointed out, you can fix your process. That your
> > process doesn't work well with Python isn't a good reason for changing
> > Python.
>
> Do you have any ideas on how to improve the process of maintaining
> imports?  Benji's suggestion of jumping around doesn't work for moving
> code and it interrupts my train of thought.  Sprinkling the code with
> import statements causes a speed penalty and a lot of clutter.
>
Is using import near where you need it really such a hit to speed ? May
be it is critical inside a function(may be called in a loop) but what
about something like this :

import <my needed modules for this_func>
def this_func:
  use it here

In this way, it is no different than the @decorator in terms of "moving
codes around", i.e. just copy the extra import/@decorator lines if you
want to move this to a new module. In terms of speed, it won't be part
of the function but a module so the speed penalty would be only when
other import this module(one time or multiple time but still not the
same as calling a function in a loop), no different than putting
everything at the top of the file(well a bit but should be
neglecgible).




More information about the Python-list mailing list