Constructed object returns from function as NoneType [closed]

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.

New contributor

Vince is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật