FAQ: Hash Maps: Python - Creating the Hashing Function

This community-built FAQ covers the “Creating the Hashing Function” exercise from the lesson “Hash Maps: Python”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Computer Science

Complex Data Structures

FAQs on the exercise Creating the Hashing Function

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Hello to all.

I am doing this exercise and I have a question about the String method encode.

In the exercise, it says that key.encode() will return an object containing a list-like object that contains the numerical value of each character as a separate item.

I tested it out with my key as “dog” and printed the result to the console. It gave me b’dog’ as a result.

I was wondering if someone could please tell me what b’dog’ signifies.

Thank you :grinning:

1 Like

@mtf please answer this

b’dog’ is a representation of a bytes object. That type of object is returned by the str.encode() method

See the documentation at str. encode ( encoding=“utf-8” , errors=“strict” ).

Here’s the result of an interactive Python session:

>>> "dog".encode()
b'dog'
>>> type("dog".encode())
<class 'bytes'>
>>> "dog".encode()[0]
100
>>> "dog".encode()[1]
111
>>> "dog".encode()[2]
103
>>> "π".encode()
b'\xcf\x80'
>>> "π".encode()[0]
207
>>> "π".encode()[1]
128

Note that each character in "dog" could be encoded by a single number between 0 and 255, but the Greek "π" is encoded by two such numbers, each representing a byte. Some characters from other languages require even more bytes than that to be encoded.

1 Like

I actually thought that we were going to learn about encoding the key but it was very straight forward by giving us a built-in method.
Works fine though :disappointed: :disappointed:

Hello Codecademy Forum,

Long time reader, first time poster here.

Although it may be somewhat digressing from the lesson material, it is bugging me why in the following method we return hash_code as opposed to self.hash_code. And with the same logic why we use self.hash_code = sum(key_bytes), as opposed to self.hash_code = sum (self.key_bytes).

I’m struggling to find the connection to when you use self.foo and just foo.

I believe that self.foo refers to parent class, so if we were to call class_name.foo, a value would be returned. I may have just answered my own question by reasoning my thought process through typing this markdown?

So I guess my final question is (sorry for letting you read my long-winded thought process), do we only use self. foo when we are assigning values to be called upon outside the class definition, then use foo willy nilly for use inside our calculations?

I appreciate the time anybody has taken to help me out here.

Thanks in advance :slight_smile:

Hey @blog7031207679!

I have faced this myself but now I’m totally comfortable with it.

See, sometimes we need to reference our own class variables in the methods, modify them or use them to calculate another value. To do this we first need to reference the class and then the instance variable separated by dot (.) .
Also, by convention we use self to reference class that’s why we pass it as the first parameter to method.

When we have passed a variable as parameter into a method or created it we use it without self.

For example, in this section you might have written this code,

class HashMap:
  def __init__(self, array_size):
    self.array_size = array_size
    self.array = [None for item in range(array_size)]

  def hash(self, key):
    key_bytes = key.encode()
    hash_code = sum(key_bytes)
    return hash_code

in the hash function we passed key as a parameter so we use it without self. Also, variables like hash_code and key_bytes were created within the method so it is used without self. If we would have referenced variables :- array_size or array we would need to self. infront of them. self refers to the class itself. So everytime we use its variables or its methods within the class we self. infront of them.

I hope it’s clear now.

Ask again if you face any problem!