Dear All, I am pretty new to Python, but it has such a good reputation that I decided to give it a try. I am slightly puzzled about the syntax modulename.function. I am going through the SciPy tutorial by Oliphant (btw, is there anywhere online a free updated version of a document of this kind?). To use SciPy I normally do the following: ~$ ipython Python 2.4.4 (#2, Jan 13 2007, 17:50:26) Type "copyright", "credits" or "license" for more information. IPython 0.7.3 -- An enhanced Interactive Python. ? -> Introduction to IPython's features. %magic -> Information about IPython's 'magic' % functions. help -> Python's own help system. object? -> Details about 'object'. ?object also works, ?? prints more. In [1]: from scipy import * However, statements like the ones in the guide: In [12]: from integrate import quad --------------------------------------------------------------------------- exceptions.ImportError Traceback (most recent call last) /home/iselllo/<ipython console> ImportError: No module named integrate do not work. So I have to use: scipy.integrate. Similarly, the function gamma is not recognized, but special.gamma is. How is this chosen by the system? Then: once I have import everything from scipy, is importing explicitly the gamma function a necessity at all? **************************************************************************** On a more general ground, how does Python compare with e.g. MatLab or Octave for scientific computing? Which are the advantages and drawbacks (sorry if this is not the right forum). Kind Regards Lorenzo
Hey Lorenzo, Your question has to do with a feature of Python called namespaces. This can be a little vague and weird to the newcomer, but it is a great strength of Python. It is also very necessary because there are so many people writing so many modules for Python. Namespaces allow two different modules to have functions with the same name without conflicting with one another. So, if module1 and module2 both have functions called sqrt, module1.sqrt and module2.sqrt can be two different functions. Using from module1 import * will load that module's sqrt into the global namespace. This may seem weird, but trust me, it is a good thing. If you googled for "python namespaces" you might get a better explanation. There is an in-depth discussion of the concept in the book "Learning Python" by Mark Lutz - most libraries should have it. The question about how python (or better scipy/numpy/matplotlib/ipython) compares with Matlab or Octave will start a war if asked in the wrong places. Most people on this list will tell you stories similar to mine: I switched from Matlab to Python midway through my Ph.D. work and it was one of the best decisions I ever made. I find writing Python code so much faster and easier that I enjoy it quite a bit. I think the only risk is that if you are closely attached to some Matlab toolbox that doesn't yet exist in Python/Scipy and friends, you will need to go through the learning process of writing some things yourself. Fortunately, Python and the number of existing modules make this process not so bad. Ryan On 2/14/07, Lorenzo Isella <lorenzo.isella@gmail.com> wrote:
Dear All, I am pretty new to Python, but it has such a good reputation that I decided to give it a try. I am slightly puzzled about the syntax modulename.function. I am going through the SciPy tutorial by Oliphant (btw, is there anywhere online a free updated version of a document of this kind?). To use SciPy I normally do the following:
~$ ipython Python 2.4.4 (#2, Jan 13 2007, 17:50:26) Type "copyright", "credits" or "license" for more information.
IPython 0.7.3 -- An enhanced Interactive Python. ? -> Introduction to IPython's features. %magic -> Information about IPython's 'magic' % functions. help -> Python's own help system. object? -> Details about 'object'. ?object also works, ?? prints more.
In [1]: from scipy import *
However, statements like the ones in the guide:
In [12]: from integrate import quad --------------------------------------------------------------------------- exceptions.ImportError Traceback (most recent call last)
/home/iselllo/<ipython console>
ImportError: No module named integrate
do not work. So I have to use: scipy.integrate. Similarly, the function gamma is not recognized, but special.gamma is. How is this chosen by the system? Then: once I have import everything from scipy, is importing explicitly the gamma function a necessity at all? **************************************************************************** On a more general ground, how does Python compare with e.g. MatLab or Octave for scientific computing? Which are the advantages and drawbacks (sorry if this is not the right forum). Kind Regards
Lorenzo _______________________________________________ SciPy-user mailing list SciPy-user@scipy.org http://projects.scipy.org/mailman/listinfo/scipy-user
Lorenzo, Here is some python documentation about modules and submodules with some examples: http://docs.python.org/tut/node8.html#SECTION008400000000000000000 "from scipy import *" imports the integrate module, which means you can use "integrate.quad(...)" If you just want to be able to do "quad(...)" then you need either "from scipy.integrate import quad" or "from scipy.integrate import *" As for python vs Matlab, I will mention one area of comparison that I am most familiar with. If you have any need for building out an interactive GUI data analysis tool, even an extremely modest one, python is a clear win. Python has available Chaco2, Matplotlib and VTK/TVTK (and others) for displaying 2D and 3D data, and then your choice of toolkits, WX, GTK, Qt (and others), for building GUI applications large or small. By contrast, building even the simplest of interactive GUI applications in Matlab is a nightmare. Lorenzo Isella wrote:
Dear All, I am pretty new to Python, but it has such a good reputation that I decided to give it a try. I am slightly puzzled about the syntax modulename.function. I am going through the SciPy tutorial by Oliphant (btw, is there anywhere online a free updated version of a document of this kind?). To use SciPy I normally do the following:
~$ ipython Python 2.4.4 (#2, Jan 13 2007, 17:50:26) Type "copyright", "credits" or "license" for more information.
IPython 0.7.3 -- An enhanced Interactive Python. ? -> Introduction to IPython's features. %magic -> Information about IPython's 'magic' % functions. help -> Python's own help system. object? -> Details about 'object'. ?object also works, ?? prints more.
In [1]: from scipy import *
However, statements like the ones in the guide:
In [12]: from integrate import quad --------------------------------------------------------------------------- exceptions.ImportError Traceback (most recent call last)
/home/iselllo/<ipython console>
ImportError: No module named integrate
do not work. So I have to use: scipy.integrate. Similarly, the function gamma is not recognized, but special.gamma is. How is this chosen by the system? Then: once I have import everything from scipy, is importing explicitly the gamma function a necessity at all? **************************************************************************** On a more general ground, how does Python compare with e.g. MatLab or Octave for scientific computing? Which are the advantages and drawbacks (sorry if this is not the right forum). Kind Regards
Lorenzo _______________________________________________ SciPy-user mailing list SciPy-user@scipy.org http://projects.scipy.org/mailman/listinfo/scipy-user
On Wed, Feb 14, 2007 at 04:47:58PM +0100, Lorenzo Isella wrote:
However, statements like the ones in the guide:
In [12]: from integrate import quad --------------------------------------------------------------------------- exceptions.ImportError Traceback (most recent call last)
/home/iselllo/<ipython console>
ImportError: No module named integrate
do not work. So I have to use: scipy.integrate. Similarly, the function gamma is not recognized, but special.gamma is.
How about from scipy.integrate import quad from scipy.special import gamma That works. But most of us non-experts don't bother importing things in bits and pieces, so I somehow am used to special.<func>, signal.<func> etc. But of course, it does make a difference if you have to use the function over and over again. And here's another cheap Python trick: <trick> import scipy g = scipy.special.gamma print g == scipy.special.gamma # should be True print g(0.5) # prints sqrt(pi) </trick> So, g is special.gamma. HTH. Kumar -- Kumar Appaiah, 462, Jamuna Hostel, Indian Institute of Technology Madras, Chennai - 600 036
participants (4)
-
Bryan Van de Ven -
Kumar Appaiah -
Lorenzo Isella -
Ryan Krauss