Compare to Primitive Types in Python - Part 1

As known primitive types executes on the object structure. Primitive types takes up less in memory space. For example C & C++ low programing languages types table like that;


When I compare types with python, compare's result soo different. Type of int object 4 byte in C, but it's varies in python. Lets demonstrate this situation with this code :

ival = 0print("ival = ",ival)
print(type(ival))
print(ival.__sizeof__())
print("#############################")
ival = 1print("ival = ",ival)
print(type(ival))
print(ival.__sizeof__())
print("#############################")
ival = 100000print("ival = ",ival)
print(type(ival))
print(ival.__sizeof__())
print("#############################")
ival = 10000000000print("ival = ",ival)
print(type(ival))
print(ival.__sizeof__())
print("#############################")
ival = 100000000000000print("ival = ",ival)
print(type(ival))
print(ival.__sizeof__())
print("#############################")
ival = 10000000000000000000print("ival = ",ival)
print(type(ival))
print(ival.__sizeof__())
print("#############################")

When you run this code, display this output;

ival =  0
<class 'int'>
12
#############################
ival =  1
<class 'int'>
14
#############################
ival =  100000
<class 'int'>
16
#############################
ival =  10000000000
<class 'int'>
18
#############################
ival =  100000000000000
<class 'int'>
20
#############################
ival =  10000000000000000000
<class 'int'>
22

#############################

Every step increase the value and increse size in memory(i prefer to python 3.x in can varies to 2.x because python is not completely backward compatitable language), but other programing languages has a limit. With this example according to storage needs increase the size on memory. If type of class(every object named with class in Python's World) increase for variable, It will be synchronized and storage enhanced in RAM with dynamicly. The important point here is when you decrease that the identified object's value, it will decrease storage size dynamicly in memory. This Python's skill soo important at this point. Many problem resource of the general memory leakage programing language desing but it's not problem for Python. So this feature provides adventages a lot of idioms and patterns. In my opinion, AI(Artificial Intelligence) projects will use that :)

ival = 10print("ival = ",ival)
print(type(ival))
print(ival.__sizeof__())
print("#############################")
ival = 10000000000000000000000000print("ival = ",ival)
print(type(ival))
print(ival.__sizeof__())
print("#############################")
ival = 10print("ival = ",ival)
print(type(ival))
print(ival.__sizeof__())
print("#############################")

Output:

ival =  10
<class 'int'>
14
#############################
ival =  10000000000000000000000000
<class 'int'>
24
#############################
ival =  10
<class 'int'>
14
#############################

I would like to emphasize initializing and decleration statement. Every decleration of the variable needs to initiliaze with value. This is very similer like the C++'s auto keyword and templates class. These features is more useful for Generic programming, where the object of an object in the source file can not be fully identified or planned. In C++ compiler when the see this declerated statement in code, compiler look up for type and run the type deduction procedure. Python's statement is different with this procedure. If you would like to define any object, you must initiliaze with primitive value. This define term named with Value initiliaze(if you assing with '0', it's name is a Zero initiliaze). If you try define without value, you will see "Unresolved Referance Error" in run time or you can only use value with the where is the necessary in statement.






Comments