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 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.
int and long were "unified" a few versions back. Before that it was possible to overflow an int through math ops.
3.x has further advanced this by eliminating long altogether and only having int.
- Python 2:
sys.maxintcontains the maximum value a Python int can hold.- On a 64-bit Python 2.7, the size is 24 bytes. Check with
sys.getsizeof().
- On a 64-bit Python 2.7, the size is 24 bytes. Check with
- Python 3:
sys.maxsizecontains the maximum size in bytes a Python int can be.- This will be gigabytes in 32 bits, and exabytes in 64 bits.
- Such a large int would have a value similar to 8 to the power of
sys.maxsize.
This PEP should help.
Bottom line is that you really shouldn't have to worry about it in python versions > 2.4
hey everyone ! i'm trying to grasp some python fundemantls and i still find the term "class" confusing in <class 'int'> , 'int' is a class name that follows the same rule as my defined classes in python but 'int' is not defined using python .
i asked chatgbt and it says : 'int' is defined/implemented in C , but how do my classes that are defined in python behave the same way as the built_in ones ?