Why TypeError: 'str' object is not callable?

James Stroud jstroud at ucla.edu
Wed Mar 22 17:30:29 EST 2006


Randall Parker wrote:
> Using Python 2.4.2 on Windows 2000 in SPE.
> 
> Getting:
> TypeError: 'str' object is not callable
> 
> on this line:
> 
> TmpErrMsg1 = "State machine %s " (StateMachineName)
> 
> In Winpdb 1.0.6 the StateMachineName is of type str in the Namespace |
> Local window of local variables. It even has the string value I expect
> of 'ExampleAO'. That string variable was originally set in another
> variable by reading a socket packet field. Then it was assigned to
> StateMachineName.
> 
> I'm not using str as a variable. I searched all my source code.
> 
> So why can't I do this?
> 
> Is there a way to test what "str" is? Maybe importing the minidom
> messed up what str is? This code used to work. I am trying to figure
> out what caused it to cease to work.
> 
> Any ideas?
> 

I know several other people have given this answer:

TmpErrMsg1 = "State machine %s " % (StateMachineName)

But it deserves comment. Note that

   py> Name = 'bob'
   py> (Name) == Name
   True

Implying that the parentheses are not neccesary. But,

   py> (Name,) == Name
   False

Which may cause some confusion because

   py> "%s" % Name == "%s" % (Name,)
   True

Implying that a tuple is not necessary. Now,

   py> Name, Name
   ('bob', 'bob')

So one would expect

   py> (Name, Name) == (Name, Name)
   True

But, by the same token, one would not expect

   py> Name, Name == (Name, Name)
   ('bob', False)

This comes from operator precedence, where == binds tighther than does 
",", and so does '%' bind tighter than ",". For example,

   py> "%s" % StateMachineName == "%s" % (StateMachineName,)
   True
   py> "%s%s" % StateMachineName, StateMachineName
   Traceback (most recent call last):
     File "<stdin>", line 1, in ?
   TypeError: not enough arguments for format string

So Beware!

James


-- 
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/



More information about the Python-list mailing list