Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Yeah, as Evan said, don't type check. Just try to use the value:</p> <pre><code>def myintfunction(value): """ Please pass an integer """ return 2 + value </code></pre> <p>That doesn't have a typecheck. It is much better! Let's see what happens when I try it:</p> <pre><code>&gt;&gt;&gt; myintfunction(5) 7 </code></pre> <p>That works, because it is an integer. Hm. Lets try some text.</p> <pre><code>&gt;&gt;&gt; myintfunction('text') Traceback (most recent call last): File "&lt;stdin&gt;", line 1, in &lt;module&gt; File "&lt;stdin&gt;", line 3, in myintfunction TypeError: unsupported operand type(s) for +: 'int' and 'str' </code></pre> <p>It shows an error, TypeError, which is what it should do anyway. If caller wants to catch that, it is possible. </p> <p>What would you do if you did a typecheck? Show an error right? So you don't have to typecheck because the error is already showing up automatically.</p> <p>Plus since you didn't typecheck, you have your function working with other types:</p> <p>Floats:</p> <pre><code>&gt;&gt;&gt; print myintfunction(2.2) 4.2 </code></pre> <p>Complex numbers:</p> <pre><code>&gt;&gt;&gt; print myintfunction(5j) (2+5j) </code></pre> <p>Decimals:</p> <pre><code>&gt;&gt;&gt; import decimal &gt;&gt;&gt; myintfunction(decimal.Decimal('15')) Decimal("17") </code></pre> <p>Even completely arbitrary objects that can add numbers!</p> <pre><code>&gt;&gt;&gt; class MyAdderClass(object): ... def __radd__(self, value): ... print 'got some value: ', value ... return 25 ... &gt;&gt;&gt; m = MyAdderClass() &gt;&gt;&gt; print myintfunction(m) got some value: 2 25 </code></pre> <p>So you clearly get nothing by typechecking. And lose a lot.</p> <hr> <h1>UPDATE:</h1> <p>Since you've edited the question, it is now clear that your application calls some upstream routine that makes sense only with ints. </p> <p>That being the case, I still think you should pass the parameter <strong>as received</strong> to the upstream function. The upstream function will deal with it correctly e.g. raising an error if it needs to. I highly <strong>doubt that</strong> your function that deals with IPs will behave strangely if you pass it a float. If you can give us the name of the library we can check that for you.</p> <p>But... If the upstream function will behave incorrectly and kill some kids if you pass it a float (I still highly doubt it), then just just call <code>int()</code> on it:</p> <pre><code>def myintfunction(value): """ Please pass an integer """ return upstreamfunction(int(value)) </code></pre> <p>You're still not typechecking, so you get most benefits of not typechecking.</p> <hr> <p>If even after all that, you really want to type check, despite it reducing your application's readability and performance for absolutely no benefit, use an <code>assert</code> to do it.</p> <pre><code>assert isinstance(...) assert type() is xxxx </code></pre> <p>That way we can turn off <code>assert</code>s and remove this <code>&lt;sarcasm&gt;</code><em>feature</em><code>&lt;/sarcasm&gt;</code> from the program by calling it as</p> <pre><code>python -OO program.py </code></pre>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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