Notation for intervals?
<p>I want to make a Python class for intervals of real numbers. Syntax most closely related to mathematical notation would be <code>Interval([a, b))</code> or, even better, <code>Interval[a, b)</code> to construct the interval of all real <code>x</code> satisfying <code>a &lt;= x &lt; b</code>.</p>

<p>Is it possible to construct a class that would handle this syntax? </p>

<p>You cannot make this exact syntax work.  But you <em>could</em> do something like this by overriding the relevant comparison methods:</p>

<pre><code>a &lt;= Interval() &lt; b
</code></pre>

<p>This whole expression could then return a new <code>Interval</code> object that includes everything greater than or equal to a and strictly less than b.  <code>Interval()</code> by itself could be interpreted as the fully open interval from negative to positive infinity (i.e. the unbounded interval of all real numbers), and <code>Interval() &lt; b</code> by itself could refer to an interval bounded from above but not from below.</p>

<p>NumPy uses a similar technique for array comparison operations (where A &lt; B means "return an array of ones and zeros that correspond to whether or not each element of A is less than the respective element of B").</p>

python