list storing variables
Dave Angel
davea at davea.name
Mon Feb 23 08:24:02 EST 2015
On 02/23/2015 07:55 AM, ast wrote:
> hi
>
>>>> a = 2; b = 5
>>>> Li = [a, b]
>>>>
>>>> Li
> [2, 5]
>>>> a=3
>>>> Li
> [2, 5]
>>>>
>
> Ok, a change in a or b doesn't impact Li. This works as expected
>
> 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 ?
>
> I dont need this feature. It is just something I am thinking about.
>
> In C language, there is &A for address of A
>
When you do an "a=3" you are rebinding a, and that has no connection to
what's in the list. If you were to modify the object that a and L1[0]
share, then yes, the modifications would affect both sides.
However, an int is immutable, so you cannot directly do it.
The simplest approximation to what you're asking is to use a list within
the list.
a = [42]; b = 65
L1 = [a, b]
a[0] = 99 #this doesn't rebind a, just changes the list
# object it is bound to
print(L1)
yields:
[[99], 65]
Clearly, instead of a list, you could use some other mutable object. In
fact, you could use something like:
class Dummy(object):
pass
a = Dummy()
a.value = 12
...
--
DaveA
More information about the Python-list
mailing list