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?
Definition and UsageThe int() function converts the specified value into an integer number.
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.
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.
Python int() function returns an integer from a given object or converts a number in a given base to a decimal.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With