AttributeError, Blossom project

https://www.codecademy.com/paths/computer-science/tracks/complex-data-structures/modules/cspath-hash-maps/projects/blossom

Hello,

after clicking on Save in this moment in the code, I’m getting AttributeError: ‘HashMap’ object has no attribute ‘array’. Could you please explain what exactly is it about? Here’s the code:

from linked_list import Node, LinkedList
from blossom_lib import flower_definitions

class HashMap:
  def __init__(self, size):
    self.array.size = size
    self.array = [LinkedList() for number in range(size)]
    
  def hash(self, key):
    return sum(key.encode())
  
  def compress(self, hash_code):
    return hash_code % self.array.size
  
  def assign(self, key, value):
    array_index = self.compress(self.hash(key))
    #self.array[array_index] = [key, value]
    payload = Node([key, value])
    list_at_array = self.array[array_index]
    for item in list_at_array:
      if key == item[0]:
        item[1] == value
        return
    list_at_array.insert(payload)
    
  def retrieve(self, key):
    array_index = self.compress(self.hash(key))
    list_at_index = self.array[array_index]
    for item in list_at_index:
      if key == item[0]:
        return key[1]
    return None

blossom = HashMap(len(flower_definitions))

Thank you.

Hi @ai-2090

As a quick note, the Traceback which Python produces when it encounters an error contains a lot more useful information beyond that which you have included.

Whilst AttributeError: ‘HashMap’ object has no attribute ‘array’. tells us what the error message was, you’ve omitted the other helpful info like the location in the code where Python thought the error was.

Complete error messages = easier troubleshooting throughout Alpha Complex. :slight_smile:

Anyway, on to the error. I’m not a Pro member, and so don’t have access to the CompSci path, but I’d say your error is in the line

self.array.size = size

You’re trying to make an assignment to an attribute size, which itself is an attribute of array. Your class, at that point, does not contain any attribute array. Hence, the error.

Even if you swap around the lines in your __init__() like so:

  def __init__(self, size):
    self.array = [LinkedList() for number in range(size)]
    self.array.size = size

you’ll still get an error, because self.array is a list type and list does not have an attribute of size

Again, as a non-Pro user I can’t see the actual instructions on the lesson so I have no idea what they’re asking you to do and thus I can’t point you in the correct direction to fix your error. :frowning: Hopefully, though, now you know where the error is you might be able to fix it yourself. :slight_smile:

(Also, bonus points to anyone who gets the reference…)

1 Like

Hi @ai-2090,

The second step includes this in the instructions:

Give HashMap a constructor that takes a size parameter. Save size into self.array_size .

Therefore, as @thepitycoder points out, this line needs to be corrected:

    self.array.size = size

You have a dot where the instructions ask for an underscore.

2 Likes

I knew that line was wrong, just not what the correct answer was! :+1:

1 Like

Thanks guys!

(just to add some characters in order to have a minimum required to post)

1 Like