Let's say that I want to implement my custom list class, and I want to override __getitem__ so that the item parameter can be initialized with a default None, and behave accordingly:
class CustomList(list): def __init__(self, iterable, default_index): self.default_index = default_index super().__init__(iterable) def __getitem__(self, item=None): if item is None: item = self._default_index return super().__getitem__(item) iterable = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] my_list = CustomList(iterable, 2) This allows for my_list[None], but it would be awesome to have something like my_list[] inherently use the default argument.
Unfortunately that raises SyntaxError, so I'm assuming that the statement is illegal at the grammar level...my question is: why? Would it conflict with some other statements?
I'm very curious about this, so thanks a bunch to anyone willing to explain!