Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #7468
| References | <4DF41735.60307@gmail.com> <it17a1$vtl$1@dough.gmane.org> <4DF422A8.303@gmail.com> |
|---|---|
| From | Ian Kelly <ian.g.kelly@gmail.com> |
| Date | 2011-06-11 21:08 -0600 |
| Subject | Re: __dict__ is neato torpedo! |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.144.1307848151.11593.python-list@python.org> (permalink) |
On Sat, Jun 11, 2011 at 8:21 PM, Andrew Berg <bahamutzero8825@gmail.com> wrote:
> On 2011.06.11 09:12 PM, Terry Reedy wrote:
>> On 6/11/2011 9:32 PM, Andrew Berg wrote:
>> > I'm pretty happy that I can copy variables and their value from one
>>
>> You are copying names and their associations, but not the objects or
>> thier values.
> Associations? The update() method copies the values; a.val1 and b.val1
> point to two different places in memory.
Incorrect. The names in b will be bound to the same objects as the
names in a, not to copies of them. For immutable objects such as
ints, this doesn't matter. For mutable objects such as lists, it can:
>>> class X(object):
... pass
...
>>> a = X()
>>> b = X()
>>> a.foo = ['apples']
>>> b.__dict__.update(a.__dict__)
>>> a.foo
['apples']
>>> b.foo
['apples']
>>> a.foo.append('oranges')
>>> a.foo
['apples', 'oranges']
>>> b.foo
['apples', 'oranges']
Cheers,
Ian
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: __dict__ is neato torpedo! Ian Kelly <ian.g.kelly@gmail.com> - 2011-06-11 21:08 -0600
csiph-web