[Tutor] str object is not callable

Prasad, Ramit ramit.prasad at jpmorgan.com
Tue Jul 10 17:48:38 CEST 2012


> This piece of code works:
> 
> Big-Mac:Classes chare$ more tmp.py
> import re
> 
> def special_match(string, search=re.compile(r'[^a-zA-Z0-9\.\ \-
> \#\$\*\@\!\%\^\&]').search):
>         #string = string.rstrip()
>         return not bool(search(string))
> 
> print special_match("admin")
> print special_match("&!*")
> print special_match("=")
> 
> Big-Mac:Classes chare$ python tmp.py
> True
> True
> False
> Big-Mac:Classes chare$
> 
> However, when I use the EXACT same code in the context of the larger code, I
> get the error
> 
>     return not bool(search(strg))
> TypeError: 'str' object is not callable
> 
> The input to the function in the larger program is the same as the first test
> in the small script that works -- "admin".
> 
> As a side note -- the rstrip call is also broken, although the string module
> is imported.  I just can't figure out why this code works in one context and
> not in another.

Usually it is best to provide the full and exact traceback and Python 
version. I suspect somewhere in your module you have a variable bool
that is conflicting with the built-in version of bool. Or you could
have passed in a different search that is a string. You can tell
which is the problem by breaking this up into two lines.

ret = search(string)
return not bool( ret )

Alternatively, you do not really need the bool, although you should 
always avoid shadowing built-ins (using any variable with the same
name as a built-in, unless you are a more advanced Python user).
You could just do the following.

return not search(string)

Oh and shadowing "string" is not a good idea if you are using the 
string module.

Why do you think rstrip is broken?

Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423

--
This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  


More information about the Tutor mailing list