On 2/14/2014 1:08 PM, dave em wrote:

He is asking a question I am having trouble answering which is how a
variable containing a value differs from a variable containing a list
or more specifically a list reference.

I tried the to explain as best I can remember is that a variable is
assigned to a specific memory location with a value inside of it.

The data model of C and other memory-oriented languages is *different* from the object model of Python and similar languages. The concept of 'memory address' is fundamental to C and absent from Python. To understand Python, one must think in terms of its referenced object model and not C's memory-address model.

Consider name-binding statements, also called assignment statements. The simplest form is
<name> = <expression>

As always, the expression evaluates to a Python object. It may be an existing object or it may by a newly created. Either way, the name is associated with the object. Subsequent to the name binding, the name, as an expression, evaluates to the object it points to.

An object can have 0 to many names associated with it. That set of names can change. A name can be associated with only one object at a time, but can be rebound to other objects.

All objects have class (type) and a value. The value of some objects can be changed (lists, sets, dicts, most user-defined classes), others are fixed (numbers, strings, tuples, frozensets).

a = 1  # a points to int object with value 1; a == 1
b = a  # b points to the same int object; b == 1 and b is a
a = '1'  # a now points to a str object with value '1'; a == '1'

c = [1,2,3]  # c points to a list object whose value is the
# given sequence of int objects; c == [1,2,3]
d = c  # d is the same list object as c
d[1] = '2'  # This associates the indicated 'slot' in the
collection object d (which is also c) with a '2' string object.

'Variable' in C (a fixed memory location with a mutable value) is quite different from 'variable' in Python. Most people in Python mean a fixed name, potentially associated with a sequence of objects. In CPython, each object would usually be at a different C location. 'Variable' in math has multiple meanings.

--
Terry Jan Reedy

--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to