Error

Chris Rebert clp2 at rebertia.com
Wed May 26 14:09:58 EDT 2010


On Wed, May 26, 2010 at 10:48 AM, William Miner <william.miner at enig.com> wrote:
> I’m relative new to python and I puzzled by the following strange (to me)
> behavior. I was taking pieces from two old scripts to build a new one. When
> I began to debug it I got the following error message:
>
> Traceback (most recent call last):
>   File
> "/Users/williamminer/ex2gen/ex2gen-3.0.5/src/ScriptDev/run_ex2gen_scan.py",
> line 38, in <module>
>     if re.search('varm',line):
> AttributeError: 'function' object has no attribute 'search'
>
> This had worked in the previous script but not the new one. I noticed that
> the new script had an additional line at the beginning (line 3)
>
> #!/usr/bin/env python
> import sys, math, os, shutil, commands, re, mpmath
> from mpmath import *
>
> When I deleted this line, the script ran. Why did the line
>
> from mpmath import *
>
> Trash the search function fro the regular expression module?

mpmath defines a function named re() which returns the real part of a
complex number. Because you used "import *", the existing value of the
global variable "re", which was the regular expression module, gets
clobbered with mpmath's re() function. Thus, you get that error when
you try to call the "search" method on what is now a function object.

This is precisely why using the `from foo import *` syntax is discouraged.

Cheers,
Chris
--
http://blog.rebertia.com



More information about the Python-list mailing list