I’m sort of newish to Python, I’ve hit a really weird problem with my dictionary that I just can’t seem to figure out.
Basically I have a class called World which has a dictionary self.islands = {} the problem is when I want to add an instance of my Island class using the method addIsland(island) in World I get the error:
AttributeError: World instance has no attribute ‘islands’
on the line:
self.islands[island.name] = island
self.islands definitely exists because I initialize it in World‘s __init__ as follows:
class World():
def __init__(showBase, self):
self.islands = {}
...
The addIsland method is:
def addIsland(self, island):
island.setWorld(self)
self.islands[island.name] = island
And that method is called from the base class’s __init__ (which main runs) as follows:
self.world = World(self)
self.setupIslands()
self.setupIslands() looks like:
def setupIslands(self):
#Island 1
island1 = IslandCross()
self.world.addIsland(island1)
Sorry if it is a bit confusing but basically I’ve got 3 classes, the base one that has a main, in it’s __init__ it calls self.world = World(self) then self.setupIslands().
World’s __init__ has the line self.islands = {} and the method addIsland() which tries to add an Island to the dictionary with the key as the island’s name (island.name) and it’s value as the island instance itself.
What am I doing wrong here? I’m pretty sure I’ve got this kind of setup working in other places in my projects.
I bet it’s something stupid and simple isn’t it 🙂
Thanks for your time,
InfinitiFizz
The self-reference is the first argument to methods.