Pychecker Re: Nested List Question
mwm at mired.org
Thu Nov 3 07:56:53 CET 2005
Roman Suzi <rnd at onego.ru> writes:
> On Thu, 3 Nov 2005, Chris McCoy wrote:
>>> gridSystemId = [[None]*columns]*rows
>> You've made gridSystemID a list of `rows` references to the SAME "inner"
>> list, so the behavior you observe is the only possible one.
>> If you want copies instead, ASK for copies...:
>> gridSystemId = [ [None]*columns for x in xrange(rows) ]
> Interesting, could not pychecker recognize such situations in Python
> code and give warnings?
Well, it could always just issue warnings everytime it saw a list
multiplied by something. But that would get annoying in the cases
where that idiom doesn't have problems - which is naturally most such
usages. Your example included one such, which is why the translation
gridSystemId = [[None for y in xrange(columns)] for x in xrange(rows)] WRONG
[None] * columns doesn't have problems. Nor does any other immutable
object. So to avoid spurious warnings, pychecker would have to know
whether the objects in the list were immutable or not. It could guess
that if the objects are builtin types, but not for other types.
Mike Meyer <mwm at mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
More information about the Python-list