[Tutor] Copying [was Re: What's in a name?]

Keith Winston keithwins at gmail.com
Sat Jan 4 07:24:40 CET 2014


Thanks for all this. I ended up using

newdict = dict(olddict), which seemed to work fine. I hadn't heard about
the copy module until now. I had heard about deep/shallow copies, though in
this particular example (all int dicts), I don't think there's a
difference...?


On Sat, Jan 4, 2014 at 12:50 AM, Mark Lawrence <breamoreboy at yahoo.co.uk>wrote:

> On 04/01/2014 05:44, Steven D'Aprano wrote:
>
>> On Fri, Jan 03, 2014 at 01:53:42PM -0500, Keith Winston wrote:
>>
>>  That's what I meant to do: make a copy when I wrote chute_nums = chutes.
>>> So
>>> I should have passed chute_nums to summarize_game, but it still wouldn't
>>> work (because it's not a copy).
>>>
>>
>> Python never makes a copy of objects when you pass them to a function or
>> assign them to a name. If you want a copy, you have to copy them
>> yourself:
>>
>> import copy
>>
>> acopy = copy.copy(something)
>>
>>
>> ought to work for just about anything. (Python reserves the right to not
>> actually make a copy in cases where it actually doesn't matter.)
>>
>> There are a couple of shortcuts for this:
>>
>> # copy a dictionary
>> new = old.copy()
>>
>> # copy a list, or tuple
>> new = old[:]  # make a slice from the start to the end
>>
>>
>>
> Please be aware of the difference between deep and shallow copies see
> http://docs.python.org/3/library/copy.html
>
> --
> My fellow Pythonistas, ask not what our language can do for you, ask what
> you can do for our language.
>
> Mark Lawrence
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>



-- 
Keith
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20140104/d3bdea17/attachment-0001.html>


More information about the Tutor mailing list