Why TypeError: 'str' object is not callable?

Discussion in 'Python' started by Randall Parker, Mar 22, 2006.

  1. Using Python 2.4.2 on Windows 2000 in SPE.

    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

    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?
    Randall Parker, Mar 22, 2006
    1. Advertisements

  2. You have a boo boo
    Should be

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


    Carl J. Van Arsdall

    Build and Release
    MontaVista Software
    Carl J. Van Arsdall, Mar 22, 2006
    1. Advertisements

  3. TmpErrMsg1 = "State machine %s " % (StateMachineName)
    Richard Townsend, Mar 22, 2006
  4. Randall Parker

    Kent Johnson Guest

    And the reason for the error message is, when you write

    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 Johnson, Mar 22, 2006
  5. 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.
    Randall Parker, Mar 22, 2006
  6. Randall Parker

    James Stroud Guest

    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

    Implying that the parentheses are not neccesary. But,

    py> (Name,) == Name

    Which may cause some confusion because

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

    Implying that a tuple is not necessary. Now,

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

    So one would expect

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

    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,)
    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 Stroud
    UCLA-DOE Institute for Genomics and Proteomics
    Box 951570
    Los Angeles, CA 90095

    James Stroud, Mar 22, 2006
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.