[Tutor] Tutor Digest, Vol 61, Issue 32

Alan Gauld alan.gauld at btinternet.com
Tue Mar 10 03:28:42 CET 2009


"WM." <wferguson1 at socal.rr.com> wrote

>> From: "Alan Gauld" <alan.gauld at btinternet.com>

>> Pyton's error message told you exactly what you had done wrong
>> although you may not have quite understood it. But do you now see
>> what the error message was telling you when it said: ...
>> Can you understand it clearly enough that when a similar error
>> comes up in future you will know what to look for and where?
>>
> No, Mr. G., I cannot.

OK, Let's revisit it to try and unravel what it is saying.

>>    File "C:\Python26\TicTacToeD.py", line 150, in main
>>      DisplayBoard(board)
>>    File "C:\Python26\TicTacToeD.py", line 68, in DisplayBoard
>>      print "\n\t", board[1], "|", board[2], "|", board[3]
>> TypeError: 'function' object is unsubscriptable

Start at the bottom and work upwards.

>> TypeError: 'function' object is unsubscriptable

unsubscriptable means you can't use the [] indexing syntax,
it is not supported by the object to which you have applied it
And the object to which you applied it is a function.

>>    File "C:\Python26\TicTacToeD.py", line 68, in DisplayBoard
>>      print "\n\t", board[1], "|", board[2], "|", board[3]

The thing you subscripted was board.
Therefore board must be a function, not something that
you can subscript.

>>    File "C:\Python26\TicTacToeD.py", line 150, in main
>>      DisplayBoard(board)

This is where you call DisplayBoard and pass in board
as an argument to DisplayBoard. So you need to look
at the code in TicTacToeD.py to see how board comes
to be a function. There are really only two possibilities:
a) Is it a function to begin with?
ie do you have code that looks like

def board(...):

or

b) Did you assign board to a function?
ie is there a line like

board =

In your case it was 'b', an assignment.

Now at this stage we have reached the end of what the Python
error message is telling us. We need to do some actual debugging
to find out what we assigned to board. Was it a function?
Should it have been?

And here experience comes into play, and we might either guess
that we intended to call the function rather than assign it - ie we
need the parens at the end. OR we check the source code we
are copying to see whether we made a mistake in typing it in.
(Which was the case here)

If that had not yielded any joy we would need to get into deeper
debugging and trace back the source of whatever we assigned
to board. Maybe using print type(....) to check at what stage
our errant value became a function. But in this case it was
not necessary. The faulty line was

    board = NewBoard    <<<------This line

And NewBoard we could tell (because it was defined as such
in our code) was a function, so we almost certainly should have
added parens.

board = NewBoard()

80% of this was deductable from the Python error message
once you get used to reading them carefully and with some
experience. That is why it's important not just to fix the problems
but to understand what Python was telling you for future reference.

If you look back the thread John Fouhy actually told you that
you probably missed some parens within 2 hours of your
original post: He just got the location wrong...

---------------
....something that doesn't support them.  It's telling you that 
'board' is
a function (as opposed to a list or a dict, for example) and functions
don't support [].

Possibly you're meant to call board:

    print "\n\t", board(1), "|", board(2), "|", board(3)

Or, alternatively, you may have assigned to it by mistake somewhere.
---------------

But it took Brett to point out where you needed to apply the parens!
Which he did by locating the original source code you had copied.

HTH,

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/ 




More information about the Tutor mailing list