~/Blog

Brandon Rozek

Photo of Brandon Rozek

PhD Student @ RPI, Writer of Tidbits, and Linux Enthusiast

NotImplemented in Python

Published on

Updated on

Warning: This post has not been modified for over 2 years. For technical posts, make sure that it is still relevant.

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.

class A:
    def __mul__(self, x):
        if not isinstance(x, A):
            return NotImplemented
        return someOperation()

Have any questions or want to chat: Reply via Email

Enjoyed this post?

Published a response to this? :