built-in function- module name clash

  • Thread starter Olivier Vierlinck
  • Start date
O

Olivier Vierlinck

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
 
J

Jason Lai

Olivier said:
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
 
A

Alex Martelli

Olivier Vierlinck said:
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
 

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,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top