{"id":23357986,"url":"https://github.com/dimanu-py/python-custom-builtin","last_synced_at":"2025-09-03T05:38:18.281Z","repository":{"id":228430017,"uuid":"773938433","full_name":"dimanu-py/python-custom-builtin","owner":"dimanu-py","description":"A custom implementation of a Python dictionary","archived":false,"fork":false,"pushed_at":"2024-03-18T18:47:10.000Z","size":9,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-13T21:47:27.791Z","etag":null,"topics":["learning-by-doing","python","python-builtin"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dimanu-py.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2024-03-18T16:57:08.000Z","updated_at":"2024-03-18T18:12:48.000Z","dependencies_parsed_at":"2024-03-18T20:00:55.875Z","dependency_job_id":"20a52be6-d3da-4321-a6bc-3a05a08a98fc","html_url":"https://github.com/dimanu-py/python-custom-builtin","commit_stats":null,"previous_names":["dimanu-py/python-custom-builtin"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dimanu-py%2Fpython-custom-builtin","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dimanu-py%2Fpython-custom-builtin/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dimanu-py%2Fpython-custom-builtin/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dimanu-py%2Fpython-custom-builtin/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dimanu-py","download_url":"https://codeload.github.com/dimanu-py/python-custom-builtin/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247721899,"owners_count":20985084,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["learning-by-doing","python","python-builtin"],"created_at":"2024-12-21T10:36:01.213Z","updated_at":"2025-04-07T20:18:24.014Z","avatar_url":"https://github.com/dimanu-py.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Creating a custom dictionary in Python\n\n[![Python](https://img.shields.io/badge/Python-3.11+-yellow?style=for-the-badge\u0026logo=python\u0026logoColor=white\u0026labelColor=101010)](https://python.org)\n\n\u003e This exercise is extracted from the book [Python Object-Oriented Programming](https://www.amazon.es/Python-Object-Oriented-Programming-maintainable-object-oriented/dp/1801077266/ref=asc_df_1801077266/?tag=googshopes-21\u0026linkCode=df0\u0026hvadid=529772228567\u0026hvpos=\u0026hvnetw=g\u0026hvrand=17210193121882028235\u0026hvpone=\u0026hvptwo=\u0026hvqmt=\u0026hvdev=c\u0026hvdvcmdl=\u0026hvlocint=\u0026hvlocphy=9181140\u0026hvtargid=pla-1286085671460\u0026psc=1\u0026mcid=a6e3b83f21283dadbc7c32fb85beaca3)\n\n## Introduction\n\nIn Python, we can use different types of collections to store data. The most common ones are lists, tuples, sets, and dictionaries.\n\nAll these built-in collections implement the `Collection` protocol, which acts as an abstraction for the different types of collections.\n\nThe `Collection` at the same time depends on the `Container`, `Iterable`, and `Sized` protocols.\n\n\u003cdetails\u003e\u003csummary\u003eContainer\u003c/summary\u003e\n\n- It defines the `__contains__` method, which is used to check if a collection contains a specific element.\n- By implementing the `__contains__` method, a collection can be used in the `in`  and `not` operators.\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\u003csummary\u003eIterable\u003c/summary\u003e\n\n- It defines the `__iter__` method, which is used to iterate over the elements of a collection.\n- By implementing the `__iter__` method, a collection can be used in a `for` loop.\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\u003csummary\u003eSized\u003c/summary\u003e\n\n- It defines the `__len__` method, which is used to get the number of elements in a collection.\n- By implementing the `__len__` method, a collection can be used in the `len` function.\n\n\u003c/details\u003e\n\n## Built-in dictionary\n\nThe class diagram of the built-in dictionary is shown below.\n\n\u003e [!IMPORTANT]\n\u003e In the [explanation of the commits section](#explanation) we will understand why this class hierarchy is important.\n\n![MappingAbstractions](https://github.com/dimanu-py/python-custom-builtin/assets/61460617/50f03d55-3e57-4726-8bd1-dbbea22ba97e)\n\n\u003e [!NOTE]\n\u003e All these class definitions can be found inside _typing.pyi_ and _builtins.pyi_ files.\n\nIn Python, we can create a dictionary in two ways using the [overload decorator](https://levelup.gitconnected.com/overload-functions-in-python-d045375cff04):\n\n1. Passing a sequence of key-value pairs to the `dict` constructor.\n    \n    ```python\n    my_dict = dict({\"a\": 42, \"b\": 43, \"c\": 44})\n    ```\n2. Passing a list of tuples with key-value pairs to the `dict` constructor.\n\n    ```python\n    my_dict = dict([(\"a\", 42), (\"b\", 43), (\"c\", 44)])\n    ```\n\n## Custom dictionary\n\nWe want to create a custom immutable dictionary. The goal is to be able to load our dictionary-like mapping once with\nits keys and values, and then use it to map the keys to their values.\n\nWe want to end up with a type hint like this:\n\n```python\nBaseMapping = Mapping[Comparable, Any]\n```\n\nWe are going to create a dictionary-like mapping from some key to an object of any type.\n\nWe've defined the key as `Comparable` because we want to be able to compare the keys and sort them in order.\n\n\u003ca name=\"explanation\"\u003e\u003c/a\u003e\n### Code explanation\n\nThe commits of the main branch explain step by step how the process is done.\n\n1. [Defining the `Comparable` protocol](https://github.com/dimanu-py/python-custom-builtin/commit/b628aeb0f8b05c9ec5e7106fb629d4ceeaafca48)\n2. [Ho to use `@overload` decorator to allow different signatures](https://github.com/dimanu-py/python-custom-builtin/commit/3291739184e3aec22f2d416914a5c4e1f976c480)\n3. [What means to inherit from `Sized`](https://github.com/dimanu-py/python-custom-builtin/commit/794d4bc9340521edc1fa5d45cf8087e85bd1ee7b)\n4. [What means to inherit from `Iterable`](https://github.com/dimanu-py/python-custom-builtin/commit/aef85ece930a808fb33caf19a0eb05640b9c596c)\n5. [What means to inherit from `Container`](https://github.com/dimanu-py/python-custom-builtin/commit/2a5267ac4038ff5c84e3482223d825da5253d2b2)\n6. [What means to inherit from `Mapping`](https://github.com/dimanu-py/python-custom-builtin/commit/eb480e038cd3094509a603ad3e68f74c8ce3cdac)\n\n### Visit my GitHub profile for more projects 🚀\n\n[![Web](https://img.shields.io/badge/GitHub-Dimanu.py-14a1f0?style=for-the-badge\u0026logo=github\u0026logoColor=white\u0026labelColor=101010)](https://github.com/dimanu-py)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdimanu-py%2Fpython-custom-builtin","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdimanu-py%2Fpython-custom-builtin","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdimanu-py%2Fpython-custom-builtin/lists"}