Multi-dimensional list initialization
Andrew Robinson
andrew3 at r3dsolutions.com
Tue Nov 6 16:14:10 EST 2012
On 11/06/2012 06:35 AM, Oscar Benjamin wrote:
>
> > In general, people don't use element multiplication (that I have
> *ever* seen) to make lists where all elements of the outer most list
> point to the same sub-*list* by reference. The most common use of the
> multiplication is to fill an array with a constant, or short list of
> constants; Hence, almost everyone has to work around the issue as
> the initial poster did by using a much longer construction.
>
> That's what I have seen as well. I've never seen an example where
> someone wanted this behaviour.
>
> >
> > The most compact notation in programming really ought to reflect the
> most *commonly* desired operation. Otherwise, we're really just
> making people do extra typing for no reason.
>
> It's not so much the typing as the fact that this a common gotcha.
> Apparently many people expect different behaviour here. I seem to
> remember finding this surprising at first.
>
:) That's true as well.
>
> >
> > Further, list comprehensions take quite a bit longer to run than low
> level copies; by a factor of roughly 10. SO, it really would be worth
> implementing the underlying logic -- even if it wasn't super easy.
> >
> > I really don't think doing a shallow copy of lists would break
> anyone's program.
> > The non-list elements, whatever they are, can be left as reference
> copies -- but any element which is a list ought to be shallow copied.
> The behavior observed in the opening post where modifying one element
> of a sub-list, modifies all elements of all sub-lists is never desired
> as far as I have ever witnessed.
>
> It is a semantic change that would, I imagine, break many things in
> subtle ways.
>
?? Do you have any guesses, how ?
>
> >
> > The underlying implementation of Python can check an object type
> trivially, and the only routine needed is a shallow list copy. So, no
> it really isn't a complicated operation to do shallow copies of lists.
>
> Yes but if you're inspecting the object to find out whether to copy it
> what do you test for? If you check for a list type what about
> subclasses? What if someone else has a custom list type that is not a
> subclass? Should there be a dunder method for this?
>
No dunder methods. :)
Custom non-subclass list types aren't a common usage for list
multiplication in any event.
At present one has to do list comprehensions for that, and that would
simply remain so.
Subclasses, however, are something I hadn't considered...
> I don't think it's such a simple problem.
>
> Oscar
>
You made a good point, Oscar; I'll have to think about the subclassing a
bit.
:)
More information about the Python-list
mailing list