467,888 Members | 1,598 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,888 developers. It's quick & easy.

Why TypeError: 'str' object is not callable?

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
  • viewed: 22034
Share:
5 Replies
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
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
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
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
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.

Similar topics

1 post views Thread by Atul Kshirsagar | last post: by
7 posts views Thread by 5HH575-UAZWKVVP-7H2H48V3 | last post: by
1 post views Thread by Paul Du Bois | last post: by
1 post views Thread by Gary Wessle | last post: by
10 posts views Thread by Charles Russell | last post: by
2 posts views Thread by AWasilenko | last post: by
33 posts views Thread by christophertidy | last post: by
reply views Thread by MrMoon | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.