Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace arrays with dictionaries in Tophat.py #17

Open
dotcomstar opened this issue Sep 9, 2020 · 0 comments
Open

Replace arrays with dictionaries in Tophat.py #17

dotcomstar opened this issue Sep 9, 2020 · 0 comments
Assignees
Labels
enhancement New feature or request teaching moment Share knowledge with the rest of the team

Comments

@dotcomstar
Copy link
Member

dotcomstar commented Sep 9, 2020

If you use Python dictionaries (the equivalent of HashMaps in Java) to keep track of Tophat traits in Tophat.py then it might be a clearer way to grab the side and direction than pulling from an array.

Theoretically, you could do something like

Old:

self.side = location[0]
self.direction = location[1]

New:

self.side = location["side"]
self.direction = location["direction"]

Second, if you change the static variable all_tophats from an array into a dictionary, then you would be able to reference tophats by their names. The calling syntax could look something like this:

Old:

all_tophats[0]

New:

all_tophats["left"]

Dictionaries work by mapping one piece of data to another in "key-value pairs." It's a little bit like magic but bear with me. In this case, we could automatically link an object's name to anything we want. Right now, we just append self to the next array slot in all_tophats. This can be nice to count the number of total tophats, but makes finding the exact one you want a pain because you don't know which number is associated with which tophat. Instead, we could put the same data self into a dictionary so that it is mapped to the string representing its name. Essentially, you would then be able to search for the tophat you want by its name.

@dotcomstar dotcomstar added enhancement New feature or request teaching moment Share knowledge with the rest of the team labels Sep 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request teaching moment Share knowledge with the rest of the team
Projects
None yet
Development

No branches or pull requests

5 participants