Python: ‘tuple’ object is not callable

This can be a bit of an obscure error, if you run into it…  It looks like this:

File ".../CCRM/", line 202, in Page_Update
    ('Nav1'       , Data.Nav1),
TypeError: 'tuple' object is not callable

In reality, it’s typically caused by accidentally forgetting a comma from the line before:

    Page_MNID = App.DB.Value('''
      WHERE True
        AND "Page_MNID" = $Page_MNID
      ('ScriptPath' , Data.ScriptPath)
      ('Nav1'       , Data.Nav1),
      ('Nav2_Icon'  , Data.Nav2_Icon),
      ('Nav2_Label' , Data.Nav2_Label),
      ('Title'      , Data.Title),
      ('Active'     , Data.Active),
      Page_MNID     = Data.Page_MNID,

Notice that line 9 is missing a comma at the end?  That causes python to see this:

tuple_object = ("ScriptPath", Data.ScriptPath")
tuple_object("Nav1" , Data.Nav1)  #eg, next tuple looks like params

Solution?  Just add the comma :)

TypeError: list indices must be integers

You start your day, happily working with dictionaries…Life is good.

>>> mydict = {'key-a': 'value-a', 'key-b': 'value-b'}
>>> mydict['key-a']

All of a sudden, storm clouds appear. Your dictionary variable accidentally gets assigned a list, and life is no longer good!

>>> mydict = {'key-a': 'value-a', 'key-b': 'value-b'}
>>> mydict['key-a']
>>> mydict = []
>>> mydict['key-a']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: list indices must be integers

The error message "TypeError: list indices must be integers" is generated when you attempt to use a non-int for a index inside the [] operator.

By the way, almost the same message is used if you do this with a tuple:

TypeError: tuple indices must be integers

What is a tuple?

I get asked this from time to time, so for the fun of it, I’ll post this:

Tuples are immutable lists in python (and other languages).  Very lightweight, unchangable arrays.  They are used for storing multiple values in one variable (usually a fixed number of values).

x = (1, 2, 3, 'abc')
-> 1
-> 2
-> 3

-> abc
-> TypeError: tuple indices must be integers, not str

Tuples are immutable lists in python (and other languages).  Very lightweight, unchangable arrays.
x = (1,2,3)
x[0] == 1
x[1] == 2
x[2] == 3
x[“bob”] == “tuple indices must be integers”

A common example is storing x,y coordinances.  You could use a tuple for that:

point = (12,38)

Python can unpack tuples also.

x,y = (12,38)
-> 12
-> 38