Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to correct "TypeError: 'NoneType' object is not subscriptable" in recursive function?

Tags:

python

def Ancestors (otu,tree):
    if tree[otu][0][0] == None:
       return []
    else:
        return [otu,tree[otu][0][0]] + Ancestors (tree[otu][0][0],tree)

The problem essentially is that at some point, the function tries to call a something which is None, this happens instead of the function returning the list that I want. I thought the if statement had accounted for that, but it would seem I was wrong. Any advice?

Traceback (most recent call last):
  File "<pyshell#41>", line 1, in <module>
    Ancestors('A',a)
  File "C:\x.py", line 129, in Ancestors
    return [otu,tree[otu][0][0]] + Ancestors (tree[otu][0][0],tree)
  File "C:\x.py", line 129, in Ancestors
    return [otu,tree[otu][0][0]] + Ancestors (tree[otu][0][0],tree)
  File "C:\x.py", line 129, in Ancestors
    return [otu,tree[otu][0][0]] + Ancestors (tree[otu][0][0],tree)
  File "C:\x.py", line 129, in Ancestors
    return [otu,tree[otu][0][0]] + Ancestors (tree[otu][0][0],tree)
  File "C:\x.py", line 126, in Ancestors
    if tree[otu][0][0] == None:
TypeError: 'NoneType' object is not subscriptable

This is what tree is

{'A': [('AD', 4.0), None, None], 'C': [('ADBFGC', 14.5), None, None], 'B': [('BF', 0.5), None, None], 'E': [('ADBFGCE', 17.0), None, None], 'D': [('AD', 4.0), None, None], 'G': [('BFG', 6.25), None, None], 'F': [('BF', 0.5), None, None], 'ADBFG': [('ADBFGC', 6.25), ('AD', 4.25), ('BFG', 2.0)], 'BF': [('BFG', 5.75), ('B', 0.5), ('F', 0.5)], 'ADBFGC': [('ADBFGCE', 2.5), ('ADBFG', 6.25), ('C', 14.5)], 'ADBFGCE': [None, ('ADBFGC', 2.5), ('E', 17.0)], 'BFG': [('ADBFG', 2.0), ('BF', 5.75), ('G', 6.25)], 'AD': [('ADBFG', 4.25), ('A', 4.0), ('D', 4.0)]}

with otu referring to any of the strings in the tree.

like image 487
TheFoxx Avatar asked Mar 23 '12 21:03

TheFoxx


People also ask

How do I fix TypeError function object is not Subscriptable?

The error “TypeError: 'function' object is not subscriptable” occurs when you try to access an item from a function. Functions cannot be indexed using square brackets. To solve this error, ensure functions have different names to variables. Always call a function before attempting to access the functions.

What is NoneType object is not Subscriptable in Python?

The TypeError: Nonetype object is not subscriptable is an error that occurs when you try to subscript an object that has a none value.

How do I fix NoneType error in Python?

The error “TypeError: 'NoneType' object is not iterable” occurs when you try to iterate over a NoneType object. Objects like list, tuple, and string are iterables, but not None. To solve this error, ensure you assign any values you want to iterate over to an iterable object.

What is “TypeError nonetype object is not subscriptable”?

There are few objects like list, dict , tuple are iterable in python. But the error “ Typeerror nonetype object is not subscriptable” occurs when they have None values and Python code access them via index or subscript. Firstly, Let’s understand with some code examples. Let’s run and see its output.

What is the nonetype error in Python?

This error occurs when you try to subscript an object having “none” value. In the above example we are trying to print the value of “NoneType” object at index [0]. If we print the data type of "mylist" variable, it returns 'NoneType' variable.

What happens if you subscript an object with none value in Python?

If you subscript any object with None value, Python will raise TypeError: ‘NoneType’ object is not subscriptable exception. The term subscript means retrieving the values using indexing.

What is a subscriptable object in Python?

In Python, the objects that implement the __getitem__ method are called subscriptable objects. For example, lists, dictionaries, tuples are all subscriptable objects.


3 Answers

This simply means that either tree, tree[otu], or tree[otu][0] evaluates to None, and as such is not subscriptable. Most likely tree[otu] or tree[otu][0]. Track it down with some simple debugging like this:

def Ancestors (otu,tree):
    try:
        tree[otu][0][0]
    except TypeError:
        print otu, tre[otu]
        raise
    #etc...

or pdb

like image 178
philofinfinitejest Avatar answered Oct 17 '22 11:10

philofinfinitejest


One of the values you pass on to Ancestors becomes None at some point, it says, so check if otu, tree, tree[otu] or tree[otu][0] are None in the beginning of the function instead of only checking tree[otu][0][0] == None. But perhaps you should reconsider your path of action and the datatype in question to see if you could improve the structure somewhat.

like image 44
Morten Kristensen Avatar answered Oct 17 '22 13:10

Morten Kristensen


What is a when you call Ancestors('A',a)? If a['A'] is None, or if a['A'][0] is None, you'd receive that exception.

like image 1
Russell Borogove Avatar answered Oct 17 '22 11:10

Russell Borogove