Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8368567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:25:16+00:00 2026-06-09T13:25:16+00:00

Hello and thank you for checking out this question. I, for a basic tile

  • 0

Hello and thank you for checking out this question. I, for a basic tile based game I am making, made my own container class that holds a matrix of items / values that represent a map in the world. To do this I learned how about operator overloading. For the most part, everything seems okay, but I am getting some results that are confusing to me.

class mapArray(object):

    def __init__(self, mapdata = None, (width,height) = None, fillnumber = 0):
        #If you are already given a map array, then you can just provide
        # that first, otherwise leave it as none. Fill number is used to change the entire 
        # map array so that the map will be a different tile type. 0 is most likely just grass.
        print "horses"
        if (mapdata == None) and (width, height) == None:
            self.mapdata = []

        elif mapdata is not None:
            self.mapdata = mapdata

        elif (width,height) is not None:
            self.mapdata = [[fillnumber] * width] * height

    def __setitem__(self, (x,y), value):
        #Allows you to assign values more intuitively with the array
        self.mapdata[y][x] = value

    def __getitem__(self, (x,y)):
        #Just reverses it so it works intuitively and the array is 
        # indexed simply like map[x,y] instead of map[y][x]
        return mapArray(self.mapdata[y][x])

    def __str__(self):
        return str(self.mapdata)

    def __repr__(self):
        return str(self.mapdata)

    def __len__(self):
        return len(self.mapdata)

The getitem works fine. I have the constructor set to either take in a given list of lists, or provide a length and width that just makes an array of that size. Here are the results I get when I provide a size for the array, as opposed to giving it my own values.

testlist1 = mapArray(None, (3,3), 4)
print testlist1
testlist1[0,1] = 5
print testlist1

This gives me these results:

[[4,4,4],[4,4,4],[4,4,4]]
[[5,4,4],[5,4,4],[5,4,4]]

The first result makes sense, but the second result seems to show a problem in my setitem method that I overrode. Why does it replace the first index of every list?

And also confusing to me is what happens when I provide my own list of lists to replace the mapdata argument.

randommap = [[1,2,3,4,5],[6,7,8,9,10],[11,12,13,14,15]]
testlist2 = mapArray(randommap)
print testlist2

That code gives me this TypeError:

def __init__(self, mapdata = None, (width,height) = None, fillnumber = 0):
TypeError: 'NoneType' object is not iterable

to me this seems like it is saying that the mapdata is not iterable when it is set to None, but shouldnt the randommap lists I provide replace the mapdata? Perhaps there is a problem with the conditional statements I have setup so that it never gets replaced. I can’t seem to narrow down what the issue is. Any help would be greatly appreciated!

I am new to operator overloading so if there are any more efficient ways to do this let me know. I know that numpy exists to do most of this for me but I wanted to do this on my own.
Thanks again!
Ryan

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T13:25:17+00:00Added an answer on June 9, 2026 at 1:25 pm

    The problem is in __init__(). Specifically this line:

    self.mapdata = [[fillnumber] * width] * height
    

    This creates height references to the same list. The fix is:

    self.mapdata = [[fillnumber] * width for x in xrange(height)]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello and thank you in advance. I know this is total noob question, and
Suppose file1 looks like this: bye bye hello thank you And file2 looks like
Hello and thank for looking at my question. I'm in need to grab some
Hello all this is my first question on stack overflow. I am developing an
Given this HTML: <button id=buttonId1 class=hello universe> <button id=buttonId2 class=hello world> What's the correct
Hello and thanks to everyone for reading my question. I've been working on a
I have this kinda template text : Hello {#Name#}, Thanks for coming blah on
Hello one question I have which I can’t find the solution to is I
Problem If the text in TRichEdit is something like this; 'hello, world'#$D#$A Then the
Quick background info: of the three database tables that are relevant for this question,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.