Re: [Tutor] From Numpy Import *

2007-11-09 Thread Evert Rol
> Thank-you!  It is important for us to avoid potential code  
> conflicts and so we'll standardize on the import   
> syntax.
>
> On a related note:
> We are using both NumPy and SciPy.  Consider the example y = Ax  
> where A is a sparse matrix.  If A is qualified as a scipy object  
> then do y and x also have to be scipy objects or can they be numpy  
> objects?


scipy is mostly written with numpy as its core, so in general you  
should be able to use numpy objects with scipy objects (not sure, but  
I guess most scipy objects will have a numpy object as their base).
The best is simply to try it out! In this particular case, x can be a  
numpy array; y is created in the process, so will be the type of  
object most suited to the outcome of the equation (ie, you cannot  
state beforehand if it's a numpy or scipy object); in this case,  
again a numpy array.
See also the scipy tutorial on the scipy.org website, where numpy and  
scipy objects are freely mixed.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] From Numpy Import *

2007-11-08 Thread Dinesh B Vadhia
Thank-you!  It is important for us to avoid potential code conflicts and so 
we'll standardize on the import  syntax.

On a related note: 
We are using both NumPy and SciPy.  Consider the example y = Ax where A is a 
sparse matrix.  If A is qualified as a scipy object then do y and x also have 
to be scipy objects or can they be numpy objects?

Dinesh


- Original Message - 
From: Michael H. Goldwasser 
To: Dinesh B Vadhia 
Cc: tutor@python.org 
Sent: Wednesday, November 07, 2007 5:37 PM
Subject: [Tutor] From Numpy Import *



On Wednesday November 7, 2007, Dinesh B Vadhia wrote: 

>Hello!  The standard Python practice for importing modules is, for example:
>
>import sys
>import os
>etc.
>
>In NumPy (and SciPy) the 'book' suggests using:
>
>from numpy import *
>from scipy import *
>
>However, when I instead use 'import numpy' it causes all sorts of errors 
> in my existing code.

The issue is the following.  The numpy module includes many definitions, for
example a class named array.   When you use the syntax,

   from numpy import *

That takes all definitions from the module and places them into your
current namespace.  At this point, it would be fine to use a command
such as 

  values = array([1.0, 2.0, 3.0])

which instantiates a (numpy) array.


If you instead use the syntax

   import numpy

things brings that module as a whole into your namespace, but to
access definitions from that module you have to give a qualified
name, for example as

  values = numpy.array([1.0, 2.0, 3.0])

You cannot simply use the word array as in the first scenario.  This
would explain why your existing code would no longer work with the
change.

>What do you suggest?

The advantage of the "from numpy import *" syntax is mostly
convenience.   However, the better style is "import numpy" precisely
becuase it does not automatically introduce many other definitions
into your current namespace.

If you were using some other package that also defined an "array" and
then you were to use the "from numpy import *", the new definition
would override the other definition.  The use of qualified names helps
to avoid these collisions and makes clear where those definitions are
coming from.

With regard,
Michael

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] From Numpy Import *

2007-11-07 Thread Kent Johnson
Michael H. Goldwasser wrote:
>from numpy import *
>import numpy

There is a third option which provides the safety/control of import 
numpy with a little less typing:
   import numpy as np
   values = np.array([1.0, 2.0, 3.0])

and you can also import just the names you need:

   from numpy import array
   values = array(...)

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] From Numpy Import *

2007-11-07 Thread Michael H. Goldwasser

On Wednesday November 7, 2007, Dinesh B Vadhia wrote: 

>Hello!  The standard Python practice for importing modules is, for example:
>
>import sys
>import os
>etc.
>
>In NumPy (and SciPy) the 'book' suggests using:
>
>from numpy import *
>from scipy import *
>
>However, when I instead use 'import numpy' it causes all sorts of errors 
> in my existing code.

The issue is the following.  The numpy module includes many definitions, for
example a class named array.   When you use the syntax,

   from numpy import *

That takes all definitions from the module and places them into your
current namespace.  At this point, it would be fine to use a command
such as 

  values = array([1.0, 2.0, 3.0])

which instantiates a (numpy) array.


If you instead use the syntax

   import numpy

things brings that module as a whole into your namespace, but to
access definitions from that module you have to give a qualified
name, for example as

  values = numpy.array([1.0, 2.0, 3.0])

You cannot simply use the word array as in the first scenario.  This
would explain why your existing code would no longer work with the
change.

>What do you suggest?

The advantage of the "from numpy import *" syntax is mostly
convenience.   However, the better style is "import numpy" precisely
becuase it does not automatically introduce many other definitions
into your current namespace.

If you were using some other package that also defined an "array" and
then you were to use the "from numpy import *", the new definition
would override the other definition.  The use of qualified names helps
to avoid these collisions and makes clear where those definitions are
coming from.

With regard,
Michael

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] From Numpy Import *

2007-11-07 Thread Dinesh B Vadhia
Hello!  The standard Python practice for importing modules is, for example:

import sys
import os
etc.

In NumPy (and SciPy) the 'book' suggests using:

from numpy import *
from scipy import *

However, when I instead use 'import numpy' it causes all sorts of errors in my 
existing code.

What do you suggest?

Dinesh
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor