namespace issue, Python vs numpy min/max problem

D

dmitrey

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".

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.
 
T

Terry Reedy

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 *

Noone should do that unless your module is **explicitly** designed to be
*-imported and you docuement that so that they know it is OK.

Now you know why * import is generally discouraged ;-).
 
S

Steven D'Aprano

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.
 
G

Gregory Ewing

Steven said:
It
only becomes your problem if you have advised people that the right way
to use your module is with import *.

And if you're advising people to do that, it would be an
extremely good idea to give your functions different names
so that they don't conflict with the builtin min and max.
 
R

Robert Kern

And if you're advising people to do that, it would be an
extremely good idea to give your functions different names
so that they don't conflict with the builtin min and max.

And we (the numpy devs) explicitly advise people *against* that.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top