AttributeError: 'NoneType' object has no attribute 'id'... I think I have tried everything

848 Views Asked by At

I know this issue has been addressed before, but none of the offered solutions seemed to work, perhaps it's the way my code works.

I have a very simple class:

class Artist:
    def __init__(self, id):
        self.id = id;

From another Python file, I create a new instance of the class and assign the Id value:

def parseartist(response):
    parsed = json.loads(response.decode('utf-8'))
    try:
        l  = parsed.get("artists", {}).get("items", {})
        if len(l) > 0:
            f = str(l[0])
            g = f.split(",")

            id = ""
            for i in g:
                if "'id':" in i:
                    idstr = i.split(" ")
                    id = idstr[2]
                    id = id.replace("'", "")
            artist = Artist(id) 
            print(artist.id)
            return artist
    except KeyError:
        print("KeyError")

The Artist id value does get assigned (since I print out its value before I return it).

Then, I attempt to use this class in another Python file, and I try to get the value of the Artist Id I get this error:

AttributeError: 'NoneType' object has no attribute 'id'

artistclass = jsonreader.parseartist(response)
print(artistclass.id)

I've iterated through Artist class using dir(), and I can see id as an attribute. Any idea what I'm doing wrong (besides writing bad code)?

0

There are 0 best solutions below