Help, multidimensional list

Christopher Koppler klapotec at chello.at
Mon Dec 29 00:31:00 EST 2003


On Sun, 28 Dec 2003 17:50:07 -0500, "Terry Reedy" <tjreedy at udel.edu>
wrote:

>
>"Christopher Koppler" <klapotec at chello.at> wrote in message
>news:jbrtuvg9fh5e0hdj0d6vseo7287r94sa7d at 4ax.com...
>> On Sun, 28 Dec 2003 14:27:41 +0000, Crawley
>> <crawley.storm at IGetTooMuchSpamAsItIs.com> wrote:
>>
>> >Im trying to create a list of lists and for some reason its not working:
>> >
>> >class Tile:
>> >    _next = { 'n':None, 'ne':None, 'e':None, 'se':None, 's':None,
>> >'sw':None, 'w':None, 'nw':None }
>> >
>> >
[snip some code]
>> >
>> >now by my reconing this should be a list of lists with each element
>being
>> >an instance of Tile,
>>
>> You only create a *class* Tile, and provide no way to create
>> *instances* of it
>
>No, the line 't=Tile()' does create a separate Tile for each grid position.
>However, there is only one class attribute shared by all instances.

Yes, thanks for clearing that up. That is what I probably meant and
didn't know how to say. Too merry Xmas this year ;-)

>
>> - so much like with static variables in other
>> languages, you only have one 'instance' here. To be able to create
>> separate instances, you need a constructor in your class to
>> instantiate every instance, like so:
>>
>> class Tile:
>>     def __init__(self):
>>         self._next = { 'n':None, 'ne':None, 'e':None, 'se':None,
>>                        's':None, 'sw':None, 'w':None, 'nw':None }
>
>You do not need __init__ for separate instances, but do need it to give
>each instance  its own map.

I think that's what I meant with the comparison to static variables,
but should have read static class attributes.

>
>>  and some references being manipulated to point to each
>> >other.  But strangly no, its actually a list of lists where each element
>is
>> >the SAME instance of Tile, so I change one, i change them ALL!
>
>As stated above, you do have separate instances but only one map attached
>to the class instead of a separate map for each instance. Koppler's
>__init__ will fix this.
>
>Terry J. Reedy
>


--
Christopher




More information about the Python-list mailing list