I kept the value of two different variables same in Python to know that their memory address will also be same by using id() function and yes the memory address of both the variables were same but I did this same process in C so I got both of them Memory address got different using (&) operator So I have to know what is actually right inside C or inside Python
CodePudding user response:
Python does things differently than in c . In c each variable relates to a memory address, and that memory address is filled with the value. Each object has to be typed at definition so that the compiler knows how to interpret the binary.
In Python, everything is an object. "variables" are just names that refer to objects. unlike c , variables don't have types, objects have types. You can have multiple names refer to the same object. example:
a = [1] (create a list object, refer to it with name a)
b = a (no new object is created, but now both a and b refer to the single list object. They will have the same id. changing the value in the list via a[0] = 2 will change both a and b, because lists are mutable)
b = 'test' (b now refers to a string object)
b = [1] (b now refers to a NEW list object, it will have a different id than a).
Python also caches integers up to a certain value and short strings. so: a = 5
b = 5
will have the same id, even if
a = [1]
b = [1]
will have different ids
CodePudding user response:
So I have to know what is actually right inside C or inside Python
These are distinct languages where each have their own syntax and sematics and rules. You cannot directly compare(the way you're trying to do) between the two.
Python
In python, when we do an assignment we're actually creating an object reference. You can confirm this with the use of is
operator in python as shown below. For example say we have:
a = [1,2,3]
b = a; //b is a reference to the object referred to by a
print(a == b) #true
print(a is b) #true
In the above example when we do the assignment b = a
we're actually creating a variable named b
that refers to the same list object to which a
is referring. This means, here only one list
object is involved and not two.
Note also that in python there maybe caching involved for small integers and strings.
C
First note that in C initialization and assignments are different things. We will use initialization in the below example:
int a = 4;
int b = a; //b is a distinct object than a but both have same value `4`
std::cout << a <<" " << b << std::endl; //4 4
std::cout << &a << " " << &b << std::endl; //0x7ffca06a0ba0 0x7ffca06a0ba4
In the above snippet, a
and b
are different objects meaning they have different addresses and hence in the output we get different addresses printed. That is, in this case there are 2 different int
objects involved.
Note also that in C , there is a way to simulate the effect as in python using references:
int a = 4;
//--v------->note the reference used here
int &b = a; //b is an alias for the same object named a
std::cout << a <<" " << b << std::endl; //4 4
std::cout << &a << " " << &b << std::endl; //0x7ffca06a0ba0 0x7ffca06a0ba0
In the above example, b
is a reference to the object named a
. Hence in the output we get the same address printed onto the console. That is, there is only one int
object involved here.