Multi-dimensional list initialization
Joshua Landau
joshua.landau.ws at gmail.com
Thu Nov 8 01:46:03 CET 2012
On 7 November 2012 23:55, Andrew Robinson <andrew3 at r3dsolutions.com> wrote:
> On 11/07/2012 05:39 AM, Joshua Landau wrote:
>
> A more modest addition for the limited case described in this thread
>> could be to use exponentiation:
>>
>> >>> [0] ** (2, 3)
>> [[0, 0, 0], [0, 0, 0]]
>>
> I'm against over using the math operators, for the reason that matrix
> and vector algebra have meanings mathematicians desire (rightly) to
> maintain. Numpy users might find matricies overloaded to do these things
> in the future -- and then it becomes unclear whether an initialization is
> happening or a mathematical operation. I think it best just not to set up
> an accident waiting to happen in the first place.
>
I'm going to say right now that I'm very much fond of
the exponentiation proposal.
Multiplication on Numpy arrays is already completely disjoint to
multiplication on lists, and that is probably completely disjoint to all
sorts of mathematical meanings. I don't personally feel that anyone who
knows what [0] * 3 is would *assume* (although they may suppose)
that exponentiation will be a maths operator.
When I saw [0] ** (2, 3), I knew what it did before I read anything else. I
know I had the context of the posts above, so it isn't a fair comparison,
but it seems really obvious an extension. It's so closely linked to * (if
not for the ambiguities, I would have preferred multiplication) that it
makes total sense. Even if you think of "4 ** 5" as "4 * 4, 5 times", there
is a direct mental link to what is happening.
> Hold on: why not just use multiplication?
>
> >>> [0] * (2, 3)
>
> Would you consider that better than [0].nest(2).nest(3) ? or [0].nest(2,3)
> ?
> (I'm against multiplication, but I'm still interested in what you find
> attractive about it.)
>
Yes.
Having [0] * 2 with a distinct but fundamentally the same (it's just gotten
extra dimensions) partner that is called in a very similar way is a good
thing.
I'd feel equally unhappy with "4 * 3" partnering with "(4).pow(3)"* as I
would with your .nest(2, 3) and I like the iterated ones even less because
I don't see it as obviously possible for them to even work.
[0].nest(2) -> [[0], [0]] ?
[[0], [0]].nest(3) -> [[0,0,0], [0,0,0]] ???
(what about 3d?)
* Even if you could write that as "4.pow(3)" because floats didn't exist or
something.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20121108/179eac9e/attachment.html>
More information about the Python-list
mailing list