+ 1

Variable types are not declared while setting a value. then how or what number of bytes are being allocated in the memory?

2nd Jul 2016, 7:30 PM
Protiti sengupta
Protiti sengupta - avatar
3 odpowiedzi
+ 2
Indeed, they only make a reference to the value, not actually hold the value. Thus with primitive data types when you define another variable with the same value as another, the new variable will point to the value already existing in memory and not create a new value, unless you want to edit the value of one of the variables which would then allocate new memory for the edited value. E.g a = 10 //New memory allocation for value 10, a points to value 10. b = 10 //Points to value already in memory a = a+1 //New value 11 allocated to new memory block, a points to value 11. b //Still points to 10 b = 12 //New allocation for value 12. //10 is no longer being referenced therefore the memory block it is in is freed. Arrays in python work like this aswell. PS: Java's string pool works like this too.
3rd Jul 2016, 7:34 AM
Gershon Fosu
Gershon Fosu - avatar
+ 1
Python manages memory automatically. In terms of how much it allocates for basic data types, it tends to be alot more than it would be for programming languages that need variables to be declared and depends on the platform version of python and the type of data. However it makes up for the increase in memory usage by automatically freeing memory blocks that do not have variables pointing to them. In python variables just point to the value they are assigned to. If no variable is pointing to a value in memory, python's garbage collection will free the memory for reuse.
2nd Jul 2016, 8:18 PM
Gershon Fosu
Gershon Fosu - avatar
+ 1
Okay. So, each variable is actually like a pointer like in C?
3rd Jul 2016, 4:52 AM
Protiti sengupta
Protiti sengupta - avatar