Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: is 'int' a type or a function?

Tags:

python

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?

like image 891
user1299784 Avatar asked Sep 18 '16 19:09

user1299784


People also ask

Is int in Python a function?

Definition and UsageThe int() function converts the specified value into an integer number.

Is int Python data type?

In Python, numeric data type represent the data which has numeric value. Numeric value can be integer, floating number or even complex numbers. These values are defined as int , float and complex class in Python. Integers – This value is represented by int class.

Is int () a function call?

Pedantically speaking, int itself is a class, which is a callable object, and when you call a class that call gets automatically converted into a call to the class's constructor method (that is, its __new__ method). But informally it's common to refer to int() as a function call or method call.

What is a int in Python?

Python int() function returns an integer from a given object or converts a number in a given base to a decimal.


1 Answers

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)

like image 113
Bharel Avatar answered Sep 23 '22 19:09

Bharel