website/content/blog/notimplemented.md

18 lines
480 B
Markdown
Raw Normal View History

2020-01-15 21:51:49 -05:00
---
2023-01-18 11:50:40 -05:00
title: "NotImplemented in Python"
2020-01-15 21:51:49 -05:00
date: 2019-10-27T23:35:17-04:00
draft: false
2022-01-02 14:24:29 -05:00
tags: [ "Python" ]
2020-01-15 21:51:49 -05:00
---
Let's say you overwrite the `__mul__` operator in a class in Python, but you don't want the function to be called for all kinds of input. You can specify the type by just returning `NotImplemented` for types you don't want.
```python
class A:
def __mul__(self, x):
if not isinstance(x, A):
return NotImplemented
return someOperation()
```