[Tutor] Good writeup on Python assignment semantics

jim stockford jim at well.com
Tue Jul 17 17:35:07 CEST 2007


as I understand things...

there's a writeup somewhere that uses the term "bind" instead
of "assign" for the operation
a = b

for example, in Python
a = 1
the value 1 now has a name of a associated with it.
b = a
the value 1 now has two names, a and b, associated with it
the value 1 exists as an object independent of the storage
for the names a or b, which are bound to the integer object 1

in C
a = 1;
creates an integer-sized area in RAM, names it a, and puts
the value 1 in that area.
b = a;
copies the value in the area named a to a new, integer-sized
area named b

in either language (pseudocode below):
a = 1
print a
<output> a
b = 1
print b
<output> 1
a = 2
print a
<output> 2
print b
<output> 1

I'm curious to know the practical value of knowing the
difference: where can one get tripped up in this case?


On Jul 17, 2007, at 7:38 AM, Kent Johnson wrote:

> Dick Moores wrote:
>> At 04:57 AM 7/17/2007, you wrote:
>>> A recent comp.lang.python thread has a good explanation of Python's
>>> assignment semantics:
>>> http://groups.google.com/group/comp.lang.python/browse_thread/ 
>>> thread/56e7d62bf66a435c/
>>
>>
>> Kent,
>>
>> Yes, interesting. But could you explain what you mean by "assignment
>> semantics"?  Semantics: <
>> http://dictionary.reference.com/search?q=semantics>
>> I have trouble relating semantics to programming.
>
> Semantics: The study or science of meaning in language.
>
> In other words, what does it mean to say
>    a = b
> in Python?
>
> Syntax is like spelling and grammar rules - if you understand syntax,
> you can write a program that will compile and run. But without
> understanding what the various programming constructs actually mean,  
> you
> will have trouble writing a program that does something useful.
>
> I think to write correct programs in any language, you must have a
> correct mental model of what the program is doing, where by 'correct' I
> mean a model that is consistent with the actual behaviour of the  
> program.
>
> Programmers coming from a background in C and C++ have a mental model  
> of
> variables as containers for values. This model works in those  
> languages,
> where variables correspond to memory locations and assignment copies a
> value from one location to another. It doesn't work in Python, where
> variables are names for values and assignment creates another name for
> the same value without copying. To write correct Python programs you
> have to have a different model for what variables are and how they
> behave. We regularly see questions on this list from people who are
> confused by assignment because they are using a faulty model.
>
> So "what does it mean?" might be interpreted as "what is a useful model
> of this operation that allows me to understand and predict its
> behaviour?", and "assignment semantics" is a shortcut for saying "a
> useful model of assignment that allows me to understand and predict its
> behaviour."
>
> Kent
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>



More information about the Tutor mailing list