built-in function- module name clash

Discussion in 'Python' started by Olivier Vierlinck, Sep 6, 2004.

  1. Hi,

    I've a python script using somecalls to the abs() built-in function.

    Now, I have to import a module named 'abs' too...
    The consequence if that line code like

    if (abs(a-b) < epsilon:
    ...
    does not work anymore, with this error msg:

    TypeError: 'module' object is not callable

    Clearly, the interpreter tries first to consider 'abs' as a module name rather than a built-in function name.
    Any idea on how to force it to call the abs() fct? Some kind of scoping or specific syntax?
    (I could rename the abs module too but ...)

    Thanks for your help,
    cheers,
    Olivier
     
    Olivier Vierlinck, Sep 6, 2004
    #1
    1. Advertising

  2. Olivier Vierlinck

    Jason Lai Guest

    Olivier Vierlinck wrote:
    > Hi,
    >
    > I've a python script using somecalls to the abs() built-in function.
    >
    > Now, I have to import a module named 'abs' too...
    > The consequence if that line code like
    >
    > if (abs(a-b) < epsilon:
    > ...
    > does not work anymore, with this error msg:
    >
    > TypeError: 'module' object is not callable
    >
    > Clearly, the interpreter tries first to consider 'abs' as a module name rather than a built-in function name.
    > Any idea on how to force it to call the abs() fct? Some kind of scoping or specific syntax?
    > (I could rename the abs module too but ...)
    >
    > Thanks for your help,
    > cheers,
    > Olivier


    __builtins__.abs(a-b)

    Not very pretty, but it works.

    - Jason
     
    Jason Lai, Sep 6, 2004
    #2
    1. Advertising

  3. Olivier Vierlinck <> wrote:

    > Hi,
    >
    > I've a python script using somecalls to the abs() built-in function.
    >
    > Now, I have to import a module named 'abs' too...
    > The consequence if that line code like
    >
    > if (abs(a-b) < epsilon:
    > ...
    > does not work anymore, with this error msg:
    >
    > TypeError: 'module' object is not callable
    >
    > Clearly, the interpreter tries first to consider 'abs' as a module name

    rather than a built-in function name.

    Nope, it just uses the latest binding you requested for name 'abs',
    whatever kind of object that may refer to, there's just one (at a given
    time in a given scope).

    > Any idea on how to force it to call the abs() fct? Some kind of scoping or

    specific syntax?
    > (I could rename the abs module too but ...)


    A name at a given time in a scope can refer to ONE thing -- so either
    'abs' names a function or it names a module, not both, in that one scope
    at one and the same time.

    You can import under a false name, no need to rename your abs.py:

    import abs as myabs

    or you can get an alias for the builtin before you trample on it:

    builtin_abs = abs
    import abs

    and perhaps restore it later if you're done naming your abs module --
    and other ways yet. But you just can't have barename 'abs' refer to two
    different thing as the same name in the same scope.


    Alex
     
    Alex Martelli, Sep 6, 2004
    #3
    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. Jacob

    class name clash

    Jacob, Feb 6, 2004, in forum: Java
    Replies:
    14
    Views:
    834
    Larry Barowski
    Feb 10, 2004
  2. Martin Bless

    Q: distutils - name clash protection?

    Martin Bless, Jul 26, 2004, in forum: Python
    Replies:
    3
    Views:
    279
    Calvin Spealman
    Aug 2, 2004
  3. George P
    Replies:
    3
    Views:
    688
    Alex Martelli
    Sep 11, 2004
  4. Joost
    Replies:
    3
    Views:
    257
    Gabriel Genellina
    Feb 5, 2007
  5. jipjip
    Replies:
    1
    Views:
    292
    Marc 'BlackJack' Rintsch
    Oct 19, 2007
Loading...

Share This Page