From: Tim H. <tim...@ie...> - 2006-10-13 13:44:51
|
Bill Baxter wrote: > On 10/13/06, Tim Hochberg <tim...@ie...> wrote: > >> For this sort of thing, I >> would just make a new module to pull together the function I want and >> use that instead. It's then easy to explain that this new module bbeconf >> (Bill Baxter's Excellent Collection Of Numeric Functions) is actually an >> amalgamation of stuff from multiple sources. >> >> # bbeconf.py >> from numpy import * >> fromnumpy.scimath import sqrt >> # possibly some other stuff to correctly handle subpackages... >> > > That does sound like a good way to do it. > Then you just tell your users to import 'eduNumpy' rather than numpy, > and you're good to go. > Added that suggestion to http://www.scipy.org/NegativeSquareRoot > > I'd like to ask one basic Python question related my previous > suggestion of doing things like "numpy.sqrt = numpy.lib.scimath.sqrt": > In python does that make it so that any module importing numpy in the > same program will now see the altered sqrt function? E.g. in my > program I do "import A,B". Module A alters numpy.sqrt. Does that > also modify how module B sees numpy.sqrt? > Indeed it does. Module imports are cached in sys.modules, so numpy is only imported once. (With some effort, you can usually get your own private copy of a module, that you could mess with to your hearts content, but I generally wouldn't recommend it). > If so then that's a very good reason not to do it that way. > > I've heard people using the term "monkey-patch" before. Is that what that is? > I believe that is what the term refers to although I'm not absolutely certain. -tim |