Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #7469 > unrolled thread
| Started by | Andrew Berg <bahamutzero8825@gmail.com> |
|---|---|
| First post | 2011-06-11 22:28 -0500 |
| Last post | 2011-06-11 23:12 -0600 |
| Articles | 4 — 3 participants |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: __dict__ is neato torpedo! Andrew Berg <bahamutzero8825@gmail.com> - 2011-06-11 22:28 -0500
Re: __dict__ is neato torpedo! Ben Finney <ben+python@benfinney.id.au> - 2011-06-12 13:40 +1000
Re: __dict__ is neato torpedo! Andrew Berg <bahamutzero8825@gmail.com> - 2011-06-11 23:32 -0500
Re: __dict__ is neato torpedo! Ian Kelly <ian.g.kelly@gmail.com> - 2011-06-11 23:12 -0600
| From | Andrew Berg <bahamutzero8825@gmail.com> |
|---|---|
| Date | 2011-06-11 22:28 -0500 |
| Subject | Re: __dict__ is neato torpedo! |
| Message-ID | <mailman.145.1307849343.11593.python-list@python.org> |
On 2011.06.11 10:08 PM, Ian Kelly wrote: > For immutable objects such as > ints, this doesn't matter. For mutable objects such as lists, it can: Well, that's confusing. How would I make actual copies?
[toc] | [next] | [standalone]
| From | Ben Finney <ben+python@benfinney.id.au> |
|---|---|
| Date | 2011-06-12 13:40 +1000 |
| Message-ID | <877h8rvhst.fsf@benfinney.id.au> |
| In reply to | #7469 |
Andrew Berg <bahamutzero8825@gmail.com> writes: > On 2011.06.11 10:08 PM, Ian Kelly wrote: > > For immutable objects such as ints, this doesn't matter. For mutable > > objects such as lists, it can: > Well, that's confusing. It's exactly the same as with an ordinary assignment (‘a = b’) in Python. You will likely want to work through the Python Tutorial <URL:http://docs.python.org/tutorial/> and experiment with the samples as you go, in order to ground the Python data model in your mind. > How would I make actual copies? At what level? You can create a new dict or a new list by feeding the esiting one to the constructor for the type. Or you can use the various methods in the ‘copy’ module depending on what you want. Be aware, though, that most Python code gets by just fine without explicitly making copies, and I hardly ever see the ‘copy’ module actually used. Work through the tutorial, understand the data model, and work with it to get better results. -- \ “Give a man a fish, and you'll feed him for a day; give him a | `\ religion, and he'll starve to death while praying for a fish.” | _o__) —Anonymous | Ben Finney
[toc] | [prev] | [next] | [standalone]
| From | Andrew Berg <bahamutzero8825@gmail.com> |
|---|---|
| Date | 2011-06-11 23:32 -0500 |
| Message-ID | <mailman.147.1307853144.11593.python-list@python.org> |
| In reply to | #7470 |
On 2011.06.11 10:40 PM, Ben Finney wrote:
> It's exactly the same as with an ordinary assignment (‘a = b’) in
> Python.
Fair enough.
> > How would I make actual copies?
> At what level?
Level? I just want to be able to create an object b with values from
dictionary a, and not have changes to a reflect b and vice-versa once b
is created.
> You can create a new dict or a new list by feeding the
> esiting one to the constructor for the type.
Not sure what you mean here. I thought you meant it copies a dictionary
when used as input in an __init__() method, but I tried that in the
interpreter and a still affects b:
>>> class testObj():
... def __init__(self,input):
... self.__dict__.update(input)
...
>>> a = dict(list=['one', 'two'], str='hello')
>>> b = testObj(a)
>>> a['list'].append('three')
>>> a
{'list': ['one', 'two', 'three'], 'str': 'hello'}
>>> b.list
['one', 'two', 'three']
> Or you can use the various
> methods in the ‘copy’ module depending on what you want.
copy.deepcopy() looks appealing, but I don't know what the docs mean by
"administrative data structures".
> Be aware, though, that most Python code gets by just fine without
> explicitly making copies, and I hardly ever see the ‘copy’ module
> actually used.
Now that I think about it, I could probably restrict myself to immutable
types inside the dictionary without much problem. Was that your point,
or did you mean something else?
[toc] | [prev] | [next] | [standalone]
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Date | 2011-06-11 23:12 -0600 |
| Message-ID | <mailman.149.1307855605.11593.python-list@python.org> |
| In reply to | #7470 |
On Sat, Jun 11, 2011 at 10:32 PM, Andrew Berg <bahamutzero8825@gmail.com> wrote:
> On 2011.06.11 10:40 PM, Ben Finney wrote:
>> It's exactly the same as with an ordinary assignment (‘a = b’) in
>> Python.
> Fair enough.
>> > How would I make actual copies?
>> At what level?
> Level? I just want to be able to create an object b with values from
> dictionary a, and not have changes to a reflect b and vice-versa once b
> is created.
It sounds like the copy.deepcopy function is what you want:
>>> from copy import deepcopy
>>> class X(object): pass
...
>>> a = X()
>>> a.fruit = ['apples']
>>> b = deepcopy(a)
>>> a.fruit
['apples']
>>> b.fruit
['apples']
>>> a.fruit.append('oranges')
>>> a.fruit
['apples', 'oranges']
>>> b.fruit
['apples']
>> Or you can use the various
>> methods in the ‘copy’ module depending on what you want.
> copy.deepcopy() looks appealing, but I don't know what the docs mean by
> "administrative data structures".
It just means that you don't always want absolutely everything copied.
For example:
>>> class World(object): pass
...
>>> class Actor(object):
... def __init__(self, world):
... self.world = world
...
>>> class Action(object):
... def __init__(self, actor):
... self.actor = actor
...
>>> a = Action(Actor(World()))
>>> b = deepcopy(a)
>>> a.actor is b.actor
False
>>> a.actor.world is b.actor.world
False
The intention here is probably that a and b should both be part of the
same World, but as you can see that is not the case; the World got
copied along with everything else. Python provides machinery to let
you avoid deep copying absolutely everything, but it's important to be
aware of cases like this.
Cheers,
Ian
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web