[Tutor] Variables and constants [was Re: working with strings inpython3]
bodsda at googlemail.com
bodsda at googlemail.com
Tue Apr 19 21:32:10 CEST 2011
And presumably cleans up the leftover object with the value of 42 when it changes to point at the 43 object?
Or does it leave all changes in memory until the program exits?
Bodsda.
Sorry for top posting, my phone won't let me change it
Sent from my BlackBerry® wireless device
-----Original Message-----
From: Steven D'Aprano <steve at pearwood.info>
Sender: tutor-bounces+bodsda=ubuntu.com at python.org
Date: Wed, 20 Apr 2011 04:24:03
To: tutor<tutor at python.org>
Subject: [Tutor] Variables and constants [was Re: working with strings in
python3]
Rance Hall wrote:
> Variables are variable, that's why we call them variable.
> Constants are constant, and that's why we call them constant.
And Python has neither variables nor constants in the sense that (say)
Pascal, C or Fortran have, even though we often use the same words.
The differences are quite deep, but they're also subtle.
In classic programming languages with variables and/or constants, the
model is that names like "x" refer to *memory locations*. If the name is
a variable, the compiler will allow you to mutate the value stored at
that memory location; if the name is a constant, it won't. But once a
name "x" is associated with memory location (say) 123456, it can never
move. But note that the "variability" or "constantness" is associated
with the *name* (the memory location), not the value.
In languages like Python, names are associated with values, without
reference to memory locations. In this case, the "variability" or
"constantness" is associated with the *value*, not the name.
Consider x = 42; x = x+1. In Pascal, C or Fortran, this will actually
change a block of memory that had the value 42 into 43 instead:
The name x points to a memory location with value 42.
Leave the name pointing to the same place, but change the value to 43
instead.
In Python, the situation is different:
The name x points to an object with value 42.
Leave the object 42 alone, but change the name x to point to an object
with value 43 instead.
--
Steven
_______________________________________________
Tutor maillist - Tutor at python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
More information about the Tutor
mailing list