I have a function that generates a level (with the layout referred to as board in the function, stored as a numpy.array). As this level needs to have a few properties, I tried to group these properties together as a class “generatedLevel”, to construct an object by using a constructor method (setter). This object has also has methods (getters) to return its values. This is the code for my class:
class generatedLevel:
def __init__(self, board, core, ring1Dims, ring2Dims):
self.board = board
self.core = core
self.ring1Dims = ring1Dims
self.ring2Dims = ring2Dims
def get_board(self):
return self.board
def get_core(self):
return self.core
def get_ring1Dims(self):
return self.ring1Dims
def get_ring2Dims(self):
return self.ring2Dims
def print_generatedLevel(self):
print(self.get_board(), "n", self.get_core(), self.get_ring1Dims(), self.get_ring2Dims())
My problem occurs in the original function, that generates the properties of this level. This function is called “generateRings” (this is the first function in a stack that should eventually generate the entire level step by step) and takes as arguments the wanted dimensions of the level. Here, after some steps, I construct an object called board, based on the parameters that I have created in this function. When I try to execute its methods, this works. However when I try to return this object, it gets returned as a NoneType.
def generateRings(dimX, dimY):
#...
board = generatedLevel(ringMap, core, ring1Dims, ring2Dims)
print(board.get_ring1Dims()) #This does work, so the object is correctly constructed
return board #This is the issue, it returns a NoneType object
This causes another function that tries to get its properties, to raise this error: File “c:PythonProjectsUntitled-1.py”, line 276, in
generateCity(10, 10)
File “c:PythonProjectsUntitled-1.py”, line 223, in generateCity
print(board.get_ring1Dims()) #This does work, so the object is correctly constructed
^^^^^^^^^^^^^^^^^^^
AttributeError: ‘NoneType’ object has no attribute ‘get_ring1Dims’
This is the code for the function that raises the error:
def generateCity(dimX, dimY):
board = generateRings(dimX, dimY)
print(board.get_ring1Dims()) #This does not work
What I tried:
So I tried to fix this problem by changing the generateRings function to this:
def generateRings(dimX, dimY):
#...
return generatedLevel(ringMap, core, ring1Dims, ring2Dims)
This stays the same:
def generateCity(dimX, dimY):
board = generateRings(dimX, dimY)
print(board.get_ring1Dims()) #This now works
In this case it does actually correctly print the “ring1Dims” property of my function, so it correctly returned the constructed object. This means that my constructor function correctly returns the object, but when I try to store this return value of my constructor inside a variable, this variable cannot be returned from a function.
However, as I will in the future be making many changes to this object (because multiple functions generate the level layer after layer), I don’t want to construct a new object each time that I try to return it with changed properties. So in this case my question really is: “How can I correctly return my (previously constructed) object?”.
This honestly does not seem to complex but I can’t find an existing answer.
Vince is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2