Hi, i am quite new to python. I am trying (only for learning) to implement af simple grid-structure, which could be used as base for a simple 2d adventure game. My idea was to have a struct-like object which could hold 5x5 objects of the same type. Well, the code should reveal my idea. The problem is that when to code is executed, the output is:
The world
A simple table
A simple table
Which should be:
The world
A small house
A simple table
I think i might need to have a totally different approach here, and i am open to suggestionsCode:class Grid: "A grid class" a = [[None for i in range(5)] for j in range(5)] def __init__(self, des): self.description = des world = Grid("The world") world.a[0][0] = Grid("A small house") world.a[0][0].a[0][0] = Grid("A simple table") print world.description print world.a[0][0].description print world.a[0][0].a[0][0].description.
I think i might have solved this problem myself.
I thought an optimization would be to make the class definition like this:
And that solved the problem. Now i just need to know whyCode:class Grid: "A grid class" def __init__(self, des, width, height): self.description = des self.a = [[None for i in range(height)] for j in range(width)]![]()
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks