<div><span class="gmail_quote">On 10/29/07, <b class="gmail_sendername">Kent Johnson</b> &lt;<a href="mailto:kent37@tds.net">kent37@tds.net</a>&gt; wrote:</span><blockquote class="gmail_quote" style="margin:0;margin-left:0.8ex;border-left:1px #ccc solid;padding-left:1ex">
Aditya Lal wrote:<br>&gt; or use types module<br>&gt;<br>&gt; import types<br>&gt;<br>&gt; if type(n) == types.IntType or type(n) == types.LongType :<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; blah!<br><br>A few notes:<br>- If you look at types.py, you find
<br>IntType = int<br>LongType = long<br><br>and so on for all the built-in types, so there is no need or advantage<br>to importing types vs<br>&nbsp;&nbsp; if type(n) == int<br><br>- Common Python practice is to prefer the least restrictive type check
<br>possible. For Dick&#39;s specific case it doesn&#39;t matter, but I generally<br>use isinstance() instead of checking for a specific type. The difference<br>is that isinstance() is true for subtypes as well as the named type. You
<br>can also pass a tuple of types to isinstance() so you can say<br>&nbsp;&nbsp; if isinstance(n, (int, long))<br><br>Kent<br><br></blockquote></div><div><br class="webkit-block-placeholder"></div>I completely agree that the check &quot; type(n) == int &quot; is very intuitive and simple. Its just that there are many more types that the basic ones and &#39;types&#39; module provide a &quot;consistent&quot; way for checking for types.&nbsp;As an example - consider a function :
<div><br class="webkit-block-placeholder"></div><div>def execMyFun( f ) :</div><div>&nbsp;&nbsp; if type(f) == types.GeneratorType :</div><div>&nbsp;&nbsp; &nbsp; &nbsp;return f.next()</div><div>&nbsp;&nbsp; elif type(f) == types.FunctionType :</div><div>&nbsp;&nbsp; &nbsp; &nbsp;return f()
</div><div>&nbsp;&nbsp; else :</div><div>&nbsp;&nbsp; &nbsp; &nbsp;raise Exception(&quot;Invalid type for f : &quot; + type(f) )</div><div><div><div><br class="webkit-block-placeholder"></div><div>Here types module came to the rescue which otherwise I would have written using exception handling. 
<i>Well ! if you have a cleaner solution do let me know.</i></div><div><br>&nbsp;</div><div>BTW, isinstance is cool :) as it checks for all subclasses as well - didn&#39;t think of that.</div><div><br>&nbsp;</div><div>--</div><div>
Aditya
</div></div></div>