[SciPy-user] Newbie and Old matlab user

Fernando Perez Fernando.Perez at colorado.edu
Thu Oct 13 11:37:25 CDT 2005


Samuel GARCIA wrote:
> thank a lot to Alan G Isaac and Fernando Perez
> yours responses exactly anwser my questions and my poor knowledge in python!
> 
> But I still have a conceptual problem of how to work interactively !
> How to do when I edit my source2.py when debugging and run again source1.
> 
> execfile can be used instead off run but not instead off from ... import  *
> 
> In my case, using module.name is not a way, otherwise the code will be to heavy 
> to read.

Keep in mind that python gives you many ways of shortening names:

from your.very.nested.package import really_obnoxiously_long_name as x

x.foo()
x.bar()

If you are going to call foo and bar a zillion times, you can either do:

foo,bar = x.foo, x.bar

or

from your.very.nested.package.really_obnoxiously_long_name import foo, bar

Note that the former gives you automatically reload capabilities, while the 
latter requires a little trick:

import foo
reload(foo)
from foo import a,b,c

Then, you do get a,b,c to properly refresh if foo changes.

I should add that this trick (which I earlier said didn't work) does work even 
for *:

import foo
reload(foo)
from foo import *

I just tested it at an ipython prompt:

In [18]: cat foo.py
top = 1

In [19]: import foo

In [20]: reload foo
-------> reload(foo)
Out[20]: <module 'foo' from 'foo.py'>

In [21]: from foo import *

In [22]: top
Out[22]: 1

In [23]: cat foo.py
top = 'surprise'

In [24]: reload foo
-------> reload(foo)
Out[24]: <module 'foo' from 'foo.py'>

In [25]: from foo import *

In [26]: top
Out[26]: 'surprise'


Still, I would almost argue that the 'from foo import *' form is only 
acceptable for interactive work, where you really want to save up everey 
keystroke you can, and you are only testing ideas, visualizing data, etc.  For 
code written in an editor and meant to survive, a bit of namespace protection 
will save you a LOT of work in the long run, believe me.

Before anyone (John Hunter, I'm looking at you :) goes grepping the ipython 
sources: yes, I have used import * a lot in the past.  I shouldn't.  I'm 
older, wiser, and know better now.  I wrote ipython in my 2nd week of learning 
python, when 'import *' looked like a really neat and convenient facility. 
It's just a trap, so avoid it.

> How do people comming from matlab world ?

I can't answer this, as I've never used matlab, but perhaps others may help 
you on that.

Cheers,

f



More information about the SciPy-user mailing list