__dict__ is neato torpedo!

Ben Finney ben+python at benfinney.id.au
Sat Jun 11 23:40:18 EDT 2011


Andrew Berg <bahamutzero8825 at 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



More information about the Python-list mailing list