built-in function- module name clash
Alex Martelli
aleaxit at yahoo.com
Mon Sep 6 06:13:35 EDT 2004
Olivier Vierlinck <Olivier.Vierlinck at iba.be> 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
More information about the Python-list
mailing list