Why TypeError: 'str' object is not callable?

R

Randall Parker

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?
 
C

Carl J. Van Arsdall

Randall said:
Using Python 2.4.2 on Windows 2000 in SPE.

Getting:
TypeError: 'str' object is not callable

on this line:
You have a boo boo
TmpErrMsg1 = "State machine %s " (StateMachineName)
Should be

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?


--

Carl J. Van Arsdall
(e-mail address removed)
Build and Release
MontaVista Software
 
K

Kent Johnson

Carl said:
You have a boo boo



Should be

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

And the reason for the error message is, when you write
a(b)

Python interprets this as, call the object a, passing the parameter b.
If a is a string - a 'str' object - Python attempts to call the string.
Strings are not callable so you get the error message you see.

Kent
 
R

Randall Parker

Argh!

I do not know what happened to the percent signs. They used to be
there. Sorry to waste the time of so many people.
 
J

James Stroud

Randall said:
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/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top