[SciPy-User] (pas de sujet)

Sebastian Haase seb.haase at gmail.com
Fri Apr 1 13:18:52 EDT 2011


On Fri, Apr 1, 2011 at 7:06 PM, Christopher Barker
<Chris.Barker at noaa.gov> wrote:
> On 4/1/11 5:00 AM, midel wrote:
>> I began using scipy a few days ago and I think a long story between us is beginning ;) I come from the Matlab/Scilab world and I have no big problem using SciPy.
>>
>> Well, in fact, I have one problem, that is why I come here ! I will try to explain.
>>
>> I have a main program in one file "verif.py" and I want it to import functions that i have defined in another file "fonctions.py". I import Scipy in the main program but I also need the scipy functions to be available in the functions and do not manage to do so.
>>
>> The main verif.py program is :
>>
>>        from scipy import *
>>        from fonction import truc
>
> I think your immediate question was answered, but a suggestion:
>
>  From "The Zen of Python":
>
> "Namespaces are one honking great idea -- let's do more of those!"
>
> Matlab puts eveyting in one namespace -- this is convenient for
> interactive use at the command line, but gets really painful after a
> while for larger project. I HIGHLY recommend that you get out of that
> habit, and use python namespaces. What that means is that you don't use
> "import *" EVER.
>
> Some module names are a bit long to type if you are calling a lot of
> functions, so you can give them shorter names when you import. For
> instance, most folks now use:
>
> import numpy as np
>
> and often
>
> import scipy as sp
>
>
> So your code would look like:
>
> The main verif.py program is :
>
>       import fonction
>
>       a=2;
>       b=fonction.truc(a);
>       print b
>
>
>
> fonction.py is :
>
>       import numpy as np
>
>       def truc(machin):
>             plouc=machin*2;
>             A=np.array([[1,2],[3,4]]);
>             return A
>
>
> Note that "from fonction import truc" is often fine, too -- you are
> being explicit about exactly where "truc" came from.
>
> Another one from "the Zen of Python"
>
> "Explicit is better than implicit"
>
> This may seem difficult and like too much typing at first, but believe
> me, it really is a better way to write code.
>
> -Chris

try

>>> import this

;-)

- Sebastian Haase



More information about the SciPy-User mailing list