[Python-Dev] Proper way to specify that a method is not defined for a type (original) (raw)

Guido van Rossum guido at python.org
Tue Jun 7 13:56:37 EDT 2016


Setting it to None in the subclass is the intended pattern. But CPython must explicitly handle that somewhere so I don't know how general it is supported. Try defining a list subclass with len set to None and see what happens. Then try the same with MutableSequence.

On Tue, Jun 7, 2016 at 10:37 AM, Ethan Furman <ethan at stoneleaf.us> wrote:

For binary methods, such as add, either do not implement or return NotImplemented if the other operand/class is not supported.

For non-binary methods, simply do not define. Except for subclasses when the super-class defines hash and the subclass is not hashable -- then set hash to None. Question: Are there any other methods that should be set to None to tell the run-time that the method is not supported? Or is this a general mechanism for subclasses to declare any method is unsupported? -- Ethan


Python-Dev mailing list Python-Dev at python.org https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/guido%40python.org

-- --Guido van Rossum (python.org/~guido) -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.python.org/pipermail/python-dev/attachments/20160607/026aa47f/attachment.html>



More information about the Python-Dev mailing list