From the course: Advanced Python: Object-Oriented Programming

Unlock the full course today

Join today to access over 24,700 courses taught by industry experts.

Mappings

Mappings

- [Instructor] Let's see how you can emulate mappings, which are like dictionaries in Python. We are going to use the abstract base class mapping from the abc collections. And this requires us to implement get item, which is a square brackets, iter and len. And then we get contains, keys, values, et cetera, et cetera. If you want mutable mapping something we can assign to, we need also to implement get, set item and delitem. So we are going to do headers like the HTP headers. HTTP headers in the HTTP specification are case insensitive, unlike dictionaries in Python. So we're importing mapping from abc collection and creating headers. We start with the headers, which is a dictionary, and we set underscore headers to a dictionary where the keys are lowercase. This is a dictionary comprehension. And now we implement the methods. So len returns how many items we have. So it is how many items we have in headers. Get item, when we get a key, we are going to lower the key, which means the…

Contents