The meaning of a = b in object oriented languages

Summercool Summercoolness at gmail.com
Tue Sep 18 01:40:57 EDT 2007




The meaning of  a = b  in object oriented languages.
====================================================

I just want to confirm that in OOP, if a is an object, then b = a is
only copying the reference.

(to make it to the most basic form:

a is 4 bytes, let's say, at memory location 0x10000000 to 0x10000003

b is 4 bytes, let's say, at memory location 0x20000000 to 0x20000003

in 0x10000000 to 0x10000003, it is the value 0xF0000000, pointing to
an object

b = a just means
copy the 4 bytes 0xF0 0x00 0x00 0x00 into 0x20000000 to 0x2000003
so that b now points to 0xF0000000 which is the same object.)


so essentially, a is just a pointer to an object.

and b = a just means that put that same pointer into b.

and that's why in Python or Ruby, it is like:

>>> a = {"a" : 1, "b" : 2}
>>> b = a
>>> a
{'a': 1, 'b': 2}
>>> b
{'a': 1, 'b': 2}
>>> a["a"] = 999
>>> a
{'a': 999, 'b': 2}
>>> b
{'a': 999, 'b': 2}

so most or all object oriented language do assignment by reference?
is there any object oriented language actually do assignment by
value?  I kind of remember in C++, if you do

Animal a, b;

a = b will actually be assignment by value.
while in Java, Python, and Ruby, there are all assignment by
reference.  ("set by reference")

Is that the case: if a is an object, then b = a is only copying the
reference?




More information about the Python-list mailing list