Error

Alister alister.ware at ntlworld.com
Wed May 26 16:11:21 EDT 2010


On Wed, 26 May 2010 11:09:58 -0700, Chris Rebert wrote:

> 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
I am fairly new to Python but had already decided to opt a style that  
would always use import XXX rather than the from XXX import * syntax.

It is nice to know that at least one of my decisions on best practice is 
good
 

as an old programmer (8 bit assembler for micro controller applications) 
picking up the baton again for fun I have to say Python is great
(I find C to be so low level I might just as well go back to assembler!).



-- 
"Linux doesn't support any sub-32-bit computers, and despite the 
occasional
 deranged people interested in retro-computing (ie Alan Cox) I doubt it
 seriously will.."

	- Linus Torvalds



More information about the Python-list mailing list