[Tutor] problem with creating paths
steve at pearwood.info
Wed Oct 17 10:39:53 EDT 2018
On Wed, Oct 17, 2018 at 03:18:00PM +0100, Shall, Sydney via Tutor wrote:
> After searching I have found this unexpected output illustrated in the
> copy-paste below.
> The type of the paths is : <class 'tuple'>
Believe Python when it tells you something is a tuple. Trust me, it
> The values of the paths are :
Here is a good trick for debugging. Start by simplifying your data, to
make it easier to see the needle in the haystack. Long paths like the
above present you with a wall of text, so (temporarily) replace each
path with a single character to cut down on the visual complexity. That
makes it easier to see what is going on.
Replacing each path with a single letter gives me:
(['a', 'b', 'c', [...] 'x', 'y'], 'z')
Notice that your value is a tuple of two items:
Item One is a list, ['a', 'b', 'c', [...] 'x', 'y']
Item Two is a string, 'z'
> There are two items that are 'wrong' in this output.
> 1. The property 'paths' is defined in the program as a list and the
> items are added using paths.append(), yet the test says that when tested
> it is a tuple.
> 2. The tuple arises by the addition of the last entry in the file, AFTER
> the closing bracket of the list which is the first item in the tuple.
> When I test the length of 'paths' I get a value of 2!
That's because it is a tuple of two items.
> I apologise for the lengthy explanation, but I am at a loss.
> I have looked for an error that might have added an item as a + and I
> find nothing.
Without seeing your code, there's no way of telling how you constructed
this value. You intended a list, and built a list (somehow!), but then
you did *something* to replace it with a tuple.
Perhaps you did:
paths = 
for some_path in something_or_rather:
then later on:
paths = (paths, another_path)
but there's a million ways you could have got the same result. And of
course you could have used any variable name... I'm assuming it is
called "paths", but you should substitute whatever name (or names!) you
> I would much appreciate any guidance as to how I should search for the
> fault or error.
Start by looking for any line of code that starts with:
and see if and where you replaced the list with a tuple. If that gets
you nowhere, start looking for *every* reference to "paths" and see what
If *that* gets you nowhere, start adding debugging code to your program.
Put assertions like this:
assert isinstance(paths, list)
in various parts of the code, then run the program and see where it
fails. That tells you that *at that point* paths is no longer a list.
E.g. something like this:
# build the paths...
paths = 
for blah blah blah blah:
assert isinstance(paths, list) # line 50 (say)
assert isinstance(paths, list) # line 53
assert isinstance(paths, list) # line 56
If the first two assertions on line 50 and 53 pass, but the third at
line 56 fails, you know that the bug is introduced somewhere between
line 53 and 56.
More information about the Tutor