Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #86215
| Date | 2015-02-23 08:24 -0500 |
|---|---|
| From | Dave Angel <davea@davea.name> |
| Subject | Re: list storing variables |
| References | <54eb2357$0$3011$426a74cc@news.free.fr> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.19069.1424697860.18130.python-list@python.org> (permalink) |
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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
list storing variables "ast" <nomail@invalid.com> - 2015-02-23 13:55 +0100
Re: list storing variables Dave Angel <davea@davea.name> - 2015-02-23 08:24 -0500
Re: list storing variables Marko Rauhamaa <marko@pacujo.net> - 2015-02-23 15:49 +0200
Re: list storing variables Peter Pearson <pkpearson@nowhere.invalid> - 2015-02-23 17:35 +0000
Re: list storing variables Marko Rauhamaa <marko@pacujo.net> - 2015-02-23 20:22 +0200
Re: list storing variables Peter Otten <__peter__@web.de> - 2015-02-23 19:41 +0100
Re: list storing variables Marko Rauhamaa <marko@pacujo.net> - 2015-02-23 21:06 +0200
Re: list storing variables Chris Angelico <rosuav@gmail.com> - 2015-02-24 06:17 +1100
Re: list storing variables Marko Rauhamaa <marko@pacujo.net> - 2015-02-23 22:25 +0200
Re: list storing variables Ian Kelly <ian.g.kelly@gmail.com> - 2015-02-23 12:29 -0700
Re: list storing variables Marko Rauhamaa <marko@pacujo.net> - 2015-02-23 22:38 +0200
Re: list storing variables Ben Finney <ben+python@benfinney.id.au> - 2015-02-24 09:03 +1100
Re: list storing variables Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-02-24 13:24 +1100
Re: list storing variables Marko Rauhamaa <marko@pacujo.net> - 2015-02-24 10:18 +0200
csiph-web