I did this in Python 3.4:
>>> type(int)
<class 'type'>
>>> int(0)
0
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?
int is a class. The type of a class is usually type.
And yes, almost all classes can be called like functions. You create what's called an instance which is an object that behaves as you defined in the class. They can have their own functions and have special attributes.
(type is also a class if you're interested but it's a special class. It's a bit complicated but you can read more on it if you'll search for metaclasses)
Reference: Python documentation for the int class
int is a built-in class/type:
>>> isinstance(int, type)
True
When you invoke int('123'), Python finds out that int itself is nota function, but then attempts to call int.__call__('123'); this itself resolves to type.__dict__['__call__']; this is called with (int, '123') as arguments. The default __call__ implementation of type tries to construct a new object of the type given as the first argument (here int, by calling the __new__ method on that type class; thus the behaviour of int('123') indirectly comes from int.__new__(int, '123'), which constructs a new int instance that has the value of the given string parsed as an integer.
It's a class. You can use help function, it is a wrapper around pydocs.
help(int)
Help on class int in module builtins:
class int(object)
| int(x=0) -> integer
| int(x, base=10) -> integer
|
| Convert a number or string to an integer, or return 0 if no arguments
| are given. If x is a number, return x.__int__(). For floating point
| numbers, this truncates towards zero.
It's both. In Python, the types have associated functions of the same name, that coerce their argument into objects of the named type, if it can be done. So, the type int <type 'int'>has a function int along with it.
__new__ and __init__ on the class object if they exist.type(3) is int is false in Python 2.1, but true since the first stage of type/class unification in Python 2.2 (which was released in 2001). So this answer is only about 15 years out of date. :-)What happens when an integer is defined in a Python script like this one?
>>> a=1
>>> a
1
When you execute the first line, the function PyInt_FromLong is called and its logic is the following:
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
If you really want to dig deeper into this, and know how int works in Python, visit Python integer objects implementation which I referred for answering this.
It's a class i.e type obviously.
But if you are confused with its use like a function, I have answer here.
It's because, in earlier versions of Python, when it was introduced, int() was a function. It might have returned an integer (in my thought). But later int() function was no longer developed
type(int)returnstype.int(0)creates an object of typeint.