Skip to content

Instantly share code, notes, and snippets.

@vitalizzare
Created December 1, 2020 03:35
Show Gist options
  • Select an option

  • Save vitalizzare/b4b6401a70635c1052dd1643e97cc574 to your computer and use it in GitHub Desktop.

Select an option

Save vitalizzare/b4b6401a70635c1052dd1643e97cc574 to your computer and use it in GitHub Desktop.
How to constrain Python using its advanced options
# 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