Created
December 1, 2020 03:35
-
-
Save vitalizzare/b4b6401a70635c1052dd1643e97cc574 to your computer and use it in GitHub Desktop.
How to constrain Python using its advanced options
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # What if there was an option | |
| # to disable the ability for lists | |
| # to respond to index -1? | |
| from collections import UserList | |
| class StrictList(UserList): | |
| '''List that does not allow index < 0''' | |
| def __getitem__(self, index): | |
| if index < 0: | |
| raise IndexError('index < 0 is not allowed') | |
| else: | |
| return super().__getitem__(index) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment