namespace issue, Python vs numpy min/max problem

Discussion in 'Python' started by dmitrey, Nov 13, 2010.

  1. dmitrey

    dmitrey Guest

    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.
    dmitrey, Nov 13, 2010
    #1
    1. Advertising

  2. dmitrey

    dmitrey Guest

    Well, I think I have found an appropriate solution.
    Regards, D.
    dmitrey, Nov 13, 2010
    #2
    1. Advertising

  3. dmitrey

    Ben James Guest

    On 13/11/2010 19:55, dmitrey wrote:
    > Well, I think I have found an appropriate solution.
    > Regards, D.


    Hi Dmitrey,

    Would you mind briefly describing your solution?

    Thanks,
    Ben
    Ben James, Nov 13, 2010
    #3
  4. dmitrey

    Terry Reedy Guest

    On 11/13/2010 2:41 PM, 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 *


    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 ;-).

    --
    Terry Jan Reedy
    Terry Reedy, Nov 13, 2010
    #4
  5. 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
    Steven D'Aprano, Nov 13, 2010
    #5
  6. Steven D'Aprano wrote:
    > 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.

    --
    Greg
    Gregory Ewing, Nov 14, 2010
    #6
  7. dmitrey

    Robert Kern Guest

    On 2010-11-14 17:37 , Gregory Ewing wrote:
    > Steven D'Aprano wrote:
    >> 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.


    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
    Robert Kern, Nov 15, 2010
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lois
    Replies:
    1
    Views:
    3,182
    Ryan Stewart
    Dec 27, 2004
  2. juergen
    Replies:
    3
    Views:
    558
    opalinski from opalpaweb
    Sep 20, 2006
  3. Albert Hopkins

    When is min(a, b) != min(b, a)?

    Albert Hopkins, Jan 21, 2008, in forum: Python
    Replies:
    31
    Views:
    811
    Albert van der Horst
    Feb 4, 2008
  4. duncan smith
    Replies:
    9
    Views:
    7,799
    Mark Dickinson
    Feb 7, 2010
  5. carmen

    Converting hrs and min to just min

    carmen, Aug 10, 2004, in forum: ASP General
    Replies:
    4
    Views:
    120
    carmen
    Aug 10, 2004
Loading...

Share This Page