A few weeks ago (on Dec 18), I send an email to the matplotlib-devel
list indicating that "from pylab import *" overrides some builtin
functions, such as min() and max(). [1]
This results from pylab enlarging its namespace greatly with statements
like "from numerix import *" and "from mlab import *". In general this
is a good thing, but it seems numarray.linear_algebra.mlab (amongst
possibly others) overrides some builtin names. Although I don't see the
benefit in this design for numarray.linear_algebra.mlab, I can live with
it, since I never do "from numarray.linear_algebra.mlab import *".
However, I (and many others here, I suspect) would like to frequently
use "from pylab import *", and it really pisses me off when I discover
that various builtins are overridden, causing mysterious errors that may
be hard to track down.
So, I offer the following patch [2] to pylab.py which fixes this. I
hereby ask for your feedback indicating if you are happy with the
current behavior, or if you prefer that pylab does not override
builtins. (Also, if you have a better way, that would be appreciated, too.)
You may check your own systems using a little script I wrote while
testing this. [3]
Cheers!
Andrew
[1]
http://sourceforge.net/mailarchive/forum.php?thread_id=6190717&forum_id=36187
[2] Patch to pylab.py, to be inserted anywhere after the last "from blah
import *", e.g. line 216.
# restore builtin functions which may have been overridden
min = getattr(sys.modules['__builtin__'],'min')
max = getattr(sys.modules['__builtin__'],'max')
sum = getattr(sys.modules['__builtin__'],'sum')
round = getattr(sys.modules['__builtin__'],'round')
abs = getattr(sys.modules['__builtin__'],'abs')
[3] Script to test for overriding of builtin names:
import sys
def check_globals():
for key in globals().keys():
if key in dir(sys.modules['__builtin__']):
if globals()[key] != getattr(sys.modules['__builtin__'],key):
print "'%s' was overridden in globals()."%key
print 'before pylab import'
check_globals()
print
from pylab import *
print 'after pylab import'
check_globals()
print
|