namespace issue, Python vs numpy min/max problem

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Sat Nov 13 23:38:46 CET 2010


On Sat, 13 Nov 2010 11:41:09 -0800, dmitrey wrote:

> hi all,
> I have the following problem:
> I have overloaded "max" function in my module (FuncDesigner); it works
> like following:
> if some data in arguments is of type "oofun" then my function works,
> elseware numpy.max() is used.
> 
> Now the problem:
> suppose someone writes
> from FuncDesigner import *
> ...
> a = max(0,1)
> 
> so it calls FuncDesigner, it calls numpy.max and result is 0 (because in
> numpy it means "max of array with single element 0 along 1st axis").
> But if user has not imported numpy then he expected calling ordinary
> Python max and thus result to be "1".

That's not your problem, that's the caller's problem. He shouldn't be 
calling import * unless he wants to import everything in your module 
directly into his namespace.

If the caller said to you:

"After executing len = x, I can't access the built-in len function any 
more, can you change x so that it magically tells when I want the built-
in behaviour and does that?"

you'd probably tell him to get lost. Doing import * is no different. It 
only becomes your problem if you have advised people that the right way 
to use your module is with import *.


> Is there any way to get rid of the problem (somehow automatically
> determine which func should be used - numpy or Python max)? The same
> issue with "min", but they are equivalent, of course.

Automatically? No.

-- 
Steven



More information about the Python-list mailing list