list storing variables

Ben Finney ben+python at benfinney.id.au
Mon Feb 23 17:03:18 EST 2015


"ast" <nomail at invalid.com> writes:

> Ok, a change in a or b doesn't impact Li. This works as expected

Because a container stores references to objects. The other references
(such as names) that object might have are not stored.

> Is there a way to define a container object able to store some
> variables so that a change of a variable make a change in this object
> content ?

As has been pointed out, Python does not have “variables” in the sense
of a named box containing a value.

What Python does have is a mapping type. You can store a key → value
mapping for each value you want to reference later.

    >>> a = 2; b = 5
    >>> foo = {'a': a, 'b': b}
    >>> foo
    {'b': 5, 'a': 2}
    >>> foo['a'] = 3
    >>> foo
    {'b': 5, 'a': 3}

You appear, though, to want this to somehow automatically track the
changes in name bindings. No, there's no way to do that except
explicitly coding it yourself.

> In C language, there is &A for address of A

There is no “address of a value” concept in Python. You access a value
by some reference, either a name or an item in a collection. When a
reference changes to reference some different value, other references
are not affected.

-- 
 \        “All opinions are not equal. Some are a very great deal more |
  `\   robust, sophisticated, and well supported in logic and argument |
_o__)                                     than others.” —Douglas Adams |
Ben Finney




More information about the Python-list mailing list