Prev: Yet Another Configuration Parser Module
Next: TypeError: _new_() takes exactly 3 arguments (2 given) - whatdoes it mean?
From: Chris Rebert on 26 May 2010 14:09 On Wed, May 26, 2010 at 10:48 AM, William Miner <william.miner(a)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
From: MRAB on 26 May 2010 14:13 William Miner 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? > > I�m running Python 2.6.2 on Mac running OS 10.6.3. > When you write: from mpmath import * you're importing all the 'public' names (by which I mean those not starting with '_') from the mpmath module. The mpmath module happens to contain a function called 're', so 're' will now refer to that function instead of the re module. That's why using "import *" is usually a bad idea.
From: D'Arcy J.M. Cain on 26 May 2010 14:14
On Wed, 26 May 2010 13:48:42 -0400 William Miner <william.miner(a)enig.com> wrote: > #!/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? What is mpmath? It isn't in the standard distribution. My guess is that it has an object called "re" that is overwriting the re module. -- D'Arcy J.M. Cain <darcy(a)druid.net> | Democracy is three wolves http://www.druid.net/darcy/ | and a sheep voting on +1 416 425 1212 (DoD#0082) (eNTP) | what's for dinner. |