Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The reason why there is no <code>is Empty</code> is astoundingly simple once you understand what the <code>is</code> operator does.</p> <p>From the <a href="http://docs.python.org/2/reference/expressions.html#is">python manual</a>:</p> <blockquote> <p>The operators <code>is</code> and <code>is not</code> test for object identity: <code>x is y</code> is true if and only if <code>x</code> and <code>y</code> are the same object. <code>x is not y</code> yields the inverse truth value.</p> </blockquote> <p>That means <code>some_sequence is Empty</code> checks whether <code>some_sequence</code> is the same object as <code>Empty</code>. That cannot work the way you suggested.</p> <p>Consider the following example:</p> <pre><code>&gt;&gt;&gt; a = [] &gt;&gt;&gt; b = {} </code></pre> <p>Now let's pretend there is this <code>is Empty</code> construct in python:</p> <pre><code>&gt;&gt;&gt; a is Empty True &gt;&gt;&gt; b is Empty True </code></pre> <p>But since the <code>is</code> operator does identity check that means that <code>a</code> and <code>b</code> are identical to <code>Empty</code>. That in turn must mean that <code>a</code> and <code>b</code> are identical, but they are not:</p> <pre><code>&gt;&gt;&gt; a is b False </code></pre> <p>So to answer your question "why is there no <code>is Empty</code> in python?": because <code>is</code> does identity check.</p> <p>In order to have the <code>is Empty</code> construct you must either hack the <code>is</code> operator to mean something else or create some magical <code>Empty</code> object which somehow detects empty collections and then be identical to them.</p> <p>Rather than asking why there is no <code>is Empty</code> you should ask why there is no builtin function <code>isempty()</code> which calls the special method <code>__isempty__()</code>.</p> <p>So instead of using implicit booleaness:</p> <pre><code>if saved: mess_up() </code></pre> <p>we have explicit empty check:</p> <pre><code>if not isempty(saved): mess_up() </code></pre> <p>where the class of <code>saved</code> has an <code>__isempty__()</code> method implemented to some sane logic.</p> <p>I find that far better than using implicit booleaness for emptyness check.</p> <p>Of course you can easily define your own <code>isempty()</code> function:</p> <pre><code>def isempty(collection): try: return collection.__isempty__() except AttributeError: # fall back to implicit booleaness but check for common pitfalls if collection is None: raise TypeError('None cannot be empty') if collection is False: raise TypeError('False cannot be empty') if collection == 0: raise TypeError('0 cannot be empty') return bool(collection) </code></pre> <p>and then define an <code>__isempty__()</code> method which returns a boolean for all your collection classes.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload