Re: [Tutor] Copying [was Re: What's in a name?]
On 01/04/2014 07:24 AM, Keith Winston wrote: I had heard about deep/shallow copies, though in this particular example (all int dicts), I don't think there's a difference...? There's none, you're right. It's only whenever inner items (fields, etc...) themselves are complex elements and mutable. Else, mutations on original items would show on copies, and conversely. But when htere are simple items only, or immutable (tuples, strings...) the ambiguity does not exist. Denis ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Copying [was Re: What's in a name?]
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 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@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -- Keith ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Copying [was Re: What's in a name?]
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@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor