Reputation: 673
I'm trying to override a function when a specific type is passed as a parameter.
def __getitem__(self, x: str):
...
This works in Python 3 but not 2. Is there an equivalent in Python 2 or some way of achieving the same functionality?
Upvotes: 0
Views: 39
Reputation: 3575
Python 2 does not have type hinting. You can just not give the type hint, and handle the body of the function however you like.
def __getitem__(self, x):
if isinstance(x, str):
return self.data[x]
elif isinstance(x, int):
return self.numbers[x]
else:
return 0
Upvotes: 1