Any other Python flaws?
Alex Martelli
aleaxit at yahoo.com
Thu Jun 14 23:49:14 CEST 2001
"jcm" <grumble at usa.net> wrote in message
news:9gb6ap$p10$1 at news.mathworks.com...
> Edward C. Jones <edcjones at erols.com> wrote:
>
> > It follows from some basic concepts of Python that
> > "array = ([0] * 10) * 10"
> > does not behave as usually intended. This is a well-known "dark
> > corner" of Python; a place it is simply best to avoid.
>
> For this example, what would you say people would usually expect?
Presumably the same as
array = [0]*100
but then, this *IS* what they get.
Maybe Edward meant
a = [[0]*10]*10
with the "well-known dark corner" that now a[i] is a[j] for all i and
j in range(10), as opposed to, say,
a = [[0]*10 for i in range(10)]
where the 'is' doesn't hold (except when i is j:-). But I'm not sure
why "it's simply best to avoid" the simple semantics that, for any
expression, [<expression>]*n gives me a list with n references to
the SAME object (result of just ONE evaluation of <expression>),
while [<expression> for i in range(n)] evaluates the expression n
separate times (whether that gives n references to the same object
being therefore strictly dependent on the expression).
Alex
More information about the Python-list
mailing list