overriding not operator in Python
<p>I cannot find the method corresponding to <code>not x</code> operator. There is one for <code>and,or,xor</code> tho. Where is it?
<a href="https://docs.python.org/3/reference/datamodel.html" rel="nofollow">https://docs.python.org/3/reference/datamodel.html</a></p>

<p>There are no hooks for <code>and</code> or <code>or</code> operators, no (as they short-circuit), and there is no <code>xor</code> operator in Python. The <code>__and__</code> and <code>__or__</code> are for the <a href="https://docs.python.org/3/reference/expressions.html#unary-arithmetic-and-bitwise-operations" rel="nofollow"><em>bitwise</em> <code>&amp;</code> and <code>|</code> operators</a>, respectively. The equivalent bitwise operator for <code>not</code> is <code>~</code> (inversion), which is handled by the <a href="https://docs.python.org/3/reference/datamodel.html#object.__invert__" rel="nofollow"><code>__invert__</code> method</a>, while <a href="https://docs.python.org/3/reference/datamodel.html#object.__xor__" rel="nofollow"><code>__xor__</code></a> covers the <code>^</code> bitwise operator.</p>

<p><code>not</code> operates on the <a href="https://docs.python.org/3/library/stdtypes.html#truth-value-testing" rel="nofollow"><em>truth-value</em> of an object</a>. If you have a container, give it a <a href="https://docs.python.org/3/reference/datamodel.html#object.__len__" rel="nofollow"><code>__len__</code> method</a>, if not give it a <a href="https://docs.python.org/3/reference/datamodel.html#object.__bool__" rel="nofollow"><code>__bool__</code> method</a>. Either one is consulted to determine if an object should be considered 'true'; <code>not</code> inverts the result of that test.</p>

<p>So if <code>__bool__</code> returns <code>True</code> or <code>__len__</code> returns an integer other than <code>0</code>, <code>not</code> will invert that to <code>False</code>, otherwise <code>not</code> produces <code>True</code>. Note that you can't make <code>not</code> return anything else but a boolean value!</p>

<p>From the documentation for <code>__bool__</code>:</p>

<blockquote>
  <p><code>__bool__</code><br>
  Called to implement truth value testing and the built-in operation <code>bool()</code>; should return <code>False</code> or <code>True</code>. When this method is not defined, <code>__len__()</code> is called, if it is defined, and the object is considered true if its result is nonzero. If a class defines neither <code>__len__()</code> nor <code>__bool__()</code>, all its instances are considered true.></p>
</blockquote>

<p>and for the <a href="https://docs.python.org/3/reference/expressions.html#boolean-operations" rel="nofollow"><code>not</code> expression</a>:</p>

<blockquote>
  <p>In the context of Boolean operations, and also when expressions are used by control flow statements, the following values are interpreted as false: <code>False</code>, <code>None</code>, numeric zero of all types, and empty strings and containers (including strings, tuples, lists, dictionaries, sets and frozensets). All other values are interpreted as true. <strong>User-defined objects can customize their truth value by providing a <code>__bool__()</code> method.</strong></p>
  
  <p>The operator <code>not</code> yields <code>True</code> if its argument is false, <code>False</code> otherwise.</p>
</blockquote>

<p><em>bold emphasis mine</em>.</p>

python operator-overloading operators