FAQ: Hash Maps: Python - Defining the Setter

This community-built FAQ covers the “Defining the Setter” 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 Defining the Setter

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!

In order for my “assign” method to run properly, I have to to write “self.array” and “self.compressor”. Without any of those, I get an error.

But I don’t have to write “self.hash”. I can just write “hash” without the “self” and the code runs fine.

Why?
Why am I allowed to not use “self” in “hash”, while being forced to use “self” in the others?
I understand what “self” does, so I’d expect to have to use it in all three (array, compressor and hash). But for some reason I don’t.

(Also, in my “hash” function, the function name is in white, instead of blue. Not sure why either.)

1 Like

That’s the key!! hash() is a Python built-in function. In this Class, you are overriding the built-in function hash() with your own version. With self, you are calling the Class method; without self, you are calling the Python function
When you get to step 6, where you are actually running the functions, put in some print statements to see what the hash value is either way. You’ll see that, without self, the Class method hash() is not even called.

3 Likes

image

Turns out you are absolutely right!
Thanks!

2 Likes

Can someone explain in simple terms what encode() is doing?