In python I came across this strange phenomena while working with itertools groupby module.
In python, variable assignment means assigning the new variable its own memor
You state:
In python, variable assignment means assigning the new variable its own memory instead of a pointer to the original memory (from my understanding if this is incorrect please let me know):
That is incorrect. Python names can have aspects that (at time) are like C variables and can also have aspects that (at times) are like C pointers. To try and say they are like one or the other is just confusing. Don't. Consider them as unique and idiomatic to Python.
Python 'variables' should more be thought of as names. More than one may refer to the same memory location even if you did not intend them to.
Example:
>>> y=7
>>> x=7
>>> x is y
True
>>> id(x)
140316099265400
>>> id(y)
140316099265400
And (due to interning, the following may be true. See PEP 237 regarding interning of short ints, but this is an implementation detail):
>>> x=9
>>> y=5+4
>>> x is y
True
The Python is operator returns True if the two are the same objects by comparing their memory address. The id function returns that address.
Consider as a final example:
>>> li1=[1,2,3]
>>> li2=[1,2,3]
>>> li1==li2
True
>>> li1 is li2
False
Even though li1 == li2, they have to be separate lists otherwise both would change if you change one, as in this example:
>>> li1=[1,2,3]
>>> li2=li1
>>> li1.append(4)
>>> li2
[1, 2, 3, 4]
>>> li1==li2
True
>>> li1 is li2
True
(Be sure to understand another classic mistake all Python programers will make sooner or later. This is caused by multiple references to a single mutable object and then expecting one reference to act like a single object.)
As jonrsharpe pointed out in the comments, read Ned Batchelders excellent Facts and myths about Python Names and Values or How to Think Like a Pythonista for more detailed overview.