Python: is 'int' a type or a function?
<p>I did this in Python 3.4:</p>

<pre><code>&gt;&gt;&gt; type(int)
&lt;class 'type'&gt;
&gt;&gt;&gt; int(0)
0
</code></pre>

<p>Now I am wondering what int actually is. Is it a type, or is it a function? Is it both? If it is both, is it also true that all types can be called like functions?</p>

<p>What happens when an integer is defined in a Python script like this one?</p>

<pre><code>&gt;&gt;&gt; a=1
&gt;&gt;&gt; a
1
</code></pre>

<p>When you execute the first line, the function <code>PyInt_FromLong</code> is called and its logic is the following:</p>

<pre><code>if integer value in range -5,256:
    return the integer object pointed by the small integers array at the
    offset (value + 5).
else:
    if no free integer object available:
        allocate new block of integer objects
    set value of the next free integer object in the current block
    of integers.
    return integer object
</code></pre>

<p>If you really want to dig deeper into this, and know how <code>int</code> works in Python, visit <a href="http://www.laurentluce.com/posts/python-integer-objects-implementation/" rel="nofollow">Python integer objects implementation</a> which I referred for answering this.</p>

python