[Tutor] Copying a mutable

Walter Prins wprins at gmail.com
Wed Jun 8 14:50:33 CEST 2011


2011/6/8 Válas Péter <sulinet at postafiok.hu>

> As far as I understand, assignment means giving a value to a variable which
> is the expression used by classical languages that have variables (such as
> Pascal or Basic). Python has no variables, since even the simpliest data is
> an object, but we still say assignment, because it is comfortable.
>

I'd submit that conceptually Python has variables, but the way it implements
it is simply slightly different from some other languages.  So conceptually
talking about assignment is quite appropriate even if as an implementation
detail it's slightly different to some other languages (due to being object
oriented and due to design choices made in the langugae.)  I'd further
submit that from a conceptual point of view assignments mostly work as
expected, for example consider this exchange:

ActivePython 2.5.2.2 (ActiveState Software Inc.) based on
Python 2.5.2 (r252:60911, Mar 27 2008, 17:57:18) [MSC v.1310 32 bit (Intel)]
on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> a = [1,2,3]
>>> b = [1,2,3]
>>> a == b
True
>>> id(a)
14423144
>>> id(b)
14425384
>>> a = (1,2,3)
>>> b = (1,2,3)
>>> id(a)
10877368
>>> id(b)
14312952
>>> a is b
False
>>> a == b
True
>>>

Note, the lists are 2 seperate objects, but contain the same values.  From a
newbies point of view they look to be the same.  Now, from an *identity*
(id) point of view they're actually not the same, however usually that's a
detail that you can ignore, and furthermore you'll normally use an equality
check to see if they're "the same", so when a newbie that checks them for
equality (using ==) he/she will get the expected result, e.g. that they're
equal (despite not being the same object).  In other words, the fact that
they're different objects are actually irrelevant in this case.  The same
goes for the tuple as shown.

In some sense, this is atually no different from a normal pascal/C variable,
where e.g. one int will have a seperate identity (address in memory heap or
on the stack) from another int, and when comparing them it would be
implicitly understood that the machine would be reading the value from the
one address and comparing it to the value at the other address and returning
the result of the value comparison.  The identities (pointers/memory
addresses) are then conceptually irrelevant.

So, I submit that the implications and effects of some types being mutable
and others being immutable and the fact that they're implemented via objects
need not lead to confusion or complication to basic notions of assignment in
Python.  Conceptually you have variables, and you can assign values to
them.  Everything else is details.

HTH,

Walter
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110608/f9faa3ed/attachment.html>


More information about the Tutor mailing list