From: Andrew S. <str...@as...> - 2004-12-19 03:36:50
|
Here's something I find surprising (and disconcerting): astraw@flygate:~$ python Python 2.3.4 (#2, Sep 24 2004, 08:39:09) [GCC 3.3.4 (Debian 1:3.3.4-12)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> min(1,2) 1 >>> from pylab import * >>> min(1,2) Traceback (most recent call last): File "<stdin>", line 1, in ? File "/usr/lib/python2.3/site-packages/numarray/linear_algebra/mlab.py", line 149, in min return minimum.reduce(m,axis) ValueError: Empty array in a ufunc that has no identity value. So, it seems that the "min" function is overridden. Digging deeper, >>> min.__module__ 'numarray.linear_algebra.mlab' OK, I see what's going on here: in an effort to achieve maximum matlab compatibility, pylab imports everything (or at least min) from numarray.linear_algebra.mlab. The trouble with this is that a simple "from pylab import *" will break a script that uses min() on scalars. While numarray may have very good reasons for overriding builtins (although I admit that I cannot see what), I think causing normal Python scripts to break by simply importing pylab into their namespace is not the way to achieve total world domination. :) This may be a numarray.linear_algebra.mlab bug/issue and I should go to their mailing list. (Although here is probably as good a place to reach them :). But, even if the good numarray folks did it this way for a reason (I'd love to know, but I should hunt on their FAQ before asking), I suggest than pylab seek to not (incompatibly) override builtins. Again, "from pylab import *" is our plan for total world domination, and I suggest we not break pure Python scripts. The general problem does not appear serious: >>> from pylab import * >>> >>> for key in globals().keys(): ... if key in dir(__builtins__): ... print "'%s' was overridden."%key ... 'round' was overridden. 'sum' was overridden. 'abs' was overridden. 'max' was overridden. 'min' was overridden. '__doc__' was overridden. '__name__' was overridden. Cursory checks of the above functions indicate all except min(), max(), and round() are compatible with normal (non-exception raising) Python use. Even round() is apparently deprecated by numarray to around(). So, basically, I think we're just talking about min() and max(). What do folks think? |