Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Maybe <code>sum_</code> looks fine alone. What if, you know, it actually is used?</p> <pre><code>#foo.py def sum_(a, b): if type(a) == type(b): return a + b </code></pre> <p></p> <pre><code>#egg.py from foo import sum_: def egg(c = 5): return sum_(3, c) </code></pre> <p></p> <pre><code>#bar.py from egg import egg def bar(): return len(egg("2")) if __name__ == "__main__": print bar() </code></pre> <p>If you ran <code>bar.py</code> you would get:</p> <pre><code>Traceback (most recent call last): File "bar.py", line 6, in &lt;module&gt; print bar() File "bar.py", line 4, in bar return len(egg("2")) TypeError: object of type 'NoneType' has no len() </code></pre> <p>See -- usually one calls a function with the intent to act on its output. If you simply "swallow" the exception and return a dummy value, who uses your code will have an hard time troubleshooting. First off, the traceback is completely useless. This alone should be enough reason.</p> <p>Who wants to fix this bug would have to first doublecheck <code>bar.py</code>, then analize <code>egg.py</code> trying to figure out where exactly the None came from. After reading <code>egg.py</code> they'll have to read <code>sum_.py</code> and hopefully notice the implicit return of <code>None</code>; only then they understand the problem: they failed the type check because of the parameter <code>egg.py</code> put in for them.</p> <p>Put a bit of actual complexity in this and thing get ugly really fast.</p> <p>Python, unlike C, is written with the <a href="http://docs.python.org/glossary.html#term-eafp" rel="nofollow noreferrer">Easier to Ask Forgiveness than Permission</a> principle in mind: <em>if something goes wrong, I'll get an exception</em>. If you pass me a <code>None</code> where I expect an actual value, things will break, the exception will happen far away from the line actually causing it and people <em>will</em> curse in your general direction in twenty different languages, then change the code to throw a suitable exception (<code>TypeError("incompatible operand type")</code>).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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