11

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?

4
  • type(int) returns type. int(0) creates an object of type int. Commented Sep 18, 2016 at 19:58
  • Im not familiar with python at all but it looks like an object Commented Sep 18, 2016 at 19:59
  • 4
    All types/classes in Python are generally callables (function-like) as well. Commented Sep 18, 2016 at 20:01
  • 1
    @SamOrozco Well, everything in Python is an object, including types and functions, so that doesn't really answer this question. It's pretty much like saying "it's a thing." Commented Sep 18, 2016 at 20:29

6 Answers 6

16

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

Sign up to request clarification or add additional context in comments.

Comments

5

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.

Comments

1

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.

Comments

0

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.

6 Comments

I couldn't find a confirmation for this. May I ask you to expand your answer a bit with some evidence and/or references?
I don't think it is both. It is callable, and calls __new__ and __init__ on the class object if they exist.
The implementation of the so called native types has evolved over time and as far as I remember the answer is not completely wrong, it just apllies to acient Python versions (before 2).
@KlausD. Right. 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. :-)
|
0

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.

Comments

-2

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

2 Comments

Do you have any citations for "when it was introduced, int() was a function" and "later int() function was no longer developed"?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.