[SciPy-user] What can be improved ?

Steve Schmerler elcorto at gmx.net
Thu May 17 07:27:44 EDT 2007


Stef Mientki wrote:

> 
> And here are some aspects, for which I still have doubts or questions:
> - from .... import *
> the use of this construct is discouraged, and gives a warning (the only 
> warning I get is from David ;-)
> Coming from Delphi, it's very common to include everything you've available,
> so you never have to worry missing something,
> and if included in the right order,
> you are guaranteed to have the best version of everything (due to 
> overrides),
> while you're still able to use older/previous libraries, by explictly 
> naming them.
> The compiler will sort out everything you don't need.
> So what's so different in Python, that I can't include everything that's 
> on my PC ?

In most cases, import * results in poorly readable code. import * inside functions
and more general importing *anything* in functions isn't a good idea because: usually
you write a function in order to execute a chunk of code more than once. So if you
call the func N times, all the importing will also be done N times which is simply
overhead (execution time + memory action). A simple-minded timing example is
attached, which shows the effect clearly (although not utilizing some nifty tricks
offerd by the timeit module). Running it (from Ipython or the cmd line), I see the
warning David mentioned. Maybe this was intruduced with Python 2.4.x and you are on
2.3.x if you don't see it?

In [16]: %run test.py
test.py:5: SyntaxWarning: import * only allowed at module level
  def f_in():
f_in:  0.302762985229
f_out:  0.0510129928589

-- 
cheers,
steve

I love deadlines. I like the whooshing sound they make as they fly by. -- Douglas Adams

-------------- next part --------------
A non-text attachment was scrubbed...
Name: test.py
Type: text/x-python
Size: 745 bytes
Desc: not available
URL: <http://mail.scipy.org/pipermail/scipy-user/attachments/20070517/cd618ddd/attachment.py>


More information about the SciPy-User mailing list