Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #29865
| References | <5126348a-8e87-493d-975c-d6273e59784c@googlegroups.com> <b6555e91-54ba-42fb-bd99-561cb971c2ce@googlegroups.com> <505F8734.2020908@davea.name> |
|---|---|
| Date | 2012-09-24 08:27 +1000 |
| Subject | Re: List Problem |
| From | Chris Angelico <rosuav@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.1160.1348439251.27098.python-list@python.org> (permalink) |
On Mon, Sep 24, 2012 at 8:03 AM, Dave Angel <d@davea.name> wrote: > blist = [alist, alist, alist] # or blist = alist * 3 (Minor point: I think you mean this.) # or blist = [alist] * 3 > Understand, this is NOT a flaw in the language. It's perfectly > reasonable to be able to do so, in fact essential in many cases, when > you want it to be the SAME item. And this is the real part. There's no other way to handle complex objects that makes as much sense. PHP's system of references and copy-on-write assignment doesn't truly cover all cases, and it can make operations unexpectedly run vastly faster or slower depending on external circumstances, which gets annoying (the first write to an assigned array has to copy the array). C simply doesn't let you move arrays around, only pointers to them, so semantics are actually pretty similar to high level languages, only in a completely different way. These sorts of issues only ever seem to crop up with nested arrays, which strengthens this next point: Deep copying is a really REALLY hairy concept. It seems so simple at first: a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] b = deepcopy(a) # b is a new list with three new sublists But then it gets messy. a = [[1, 2, 3]]*2 + [[7, 8, 9]] It's much better to make copying a very explicit thing; it's so expensive that you really should make it very clear in your code when this happens. ChrisA
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
List Problem jimbo1qaz <jimmyli1528@gmail.com> - 2012-09-23 14:31 -0700
Re: List Problem jimbo1qaz <jimmyli1528@gmail.com> - 2012-09-23 14:44 -0700
Re: List Problem Chris Angelico <rosuav@gmail.com> - 2012-09-24 07:57 +1000
Re: List Problem Dave Angel <d@davea.name> - 2012-09-23 18:03 -0400
Re: List Problem Chris Angelico <rosuav@gmail.com> - 2012-09-24 08:27 +1000
Re: List Problem Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-09-23 22:37 +0000
Re: List Problem Chris Angelico <rosuav@gmail.com> - 2012-09-24 08:45 +1000
Re: List Problem Chris Angelico <rosuav@gmail.com> - 2012-09-24 08:30 +1000
Re: List Problem "Littlefield, Tyler" <tyler@tysdomain.com> - 2012-09-23 18:56 -0600
Re: List Problem Chris Angelico <rosuav@gmail.com> - 2012-09-24 11:52 +1000
Re: List Problem Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-09-23 22:07 +0000
Re: List Problem jimbo1qaz <jimmyli1528@gmail.com> - 2012-09-23 15:44 -0700
csiph-web