By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,961 Members | 1,320 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,961 IT Pros & Developers. It's quick & easy.

Why TypeError: 'str' object is not callable?

P: n/a
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?

Mar 22 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Randall Parker wrote:
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
cv*********@mvista.com
Build and Release
MontaVista Software

Mar 22 '06 #2

P: n/a
On 22 Mar 2006 12:10:49 -0800, Randall Parker wrote:

TmpErrMsg1 = "State machine %s " (StateMachineName)


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

--
Richard
Mar 22 '06 #3

P: n/a
Carl J. Van Arsdall wrote:
Randall Parker wrote:

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)


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
Mar 22 '06 #4

P: n/a
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.

Mar 22 '06 #5

P: n/a
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/
Mar 22 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.