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 8747665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:23:39+00:00 2026-06-13T12:23:39+00:00

I’m trying to create a simple Game of Life by storing Cell objects into

  • 0

I’m trying to create a simple Game of Life by storing Cell objects into a set (have to use classes) and I reach the problem in which I cannot add the cell object into the Set because it is unhashable… is there any way around this? Thanks!

class Cell():
    def __init__(self, row, col):
        self.row = row
        self.col = col
    def getRow(self):
        return self.row
    def getCol(self):
        return self.col
    def __eq__(self, other):
        if isinstance(other, self.__class__):
            return self.__dict__ == other.__dict__
        else:
            return False

    def __ne__(self, other):
        return not self.__eq__(other)    

class SparseLifeGrid(set):
    # Creates a new infinite-sized game grid with all cells set to dead.
    def __init__(self):
        self.rowList = []
        self.colList = []
        self.cellSet = set()

    def __add__(self, cell):
        self.cellSet.add(cell)
        self.rowList.append(cell.getRow())     
        self.colList.append(cell.getCol())     

    def minRange(self):
        #Returns a 2-tuple (minrow, mincol) that contains the minimum
        #row index and the minimum
        #column index that is currently occupied by a live cell.
        #None is returned if there are no alive cells.        
        return (sorted(self.rowList)[0], sorted(self.rowList)[0])

    def maxRange(self):
        #Returns a 2-tuple (maxrow, maxcol) that contains the
        #maximum row index and the maximum
        #column index that is currently occupied by a live cell.
        #None is returned if there are no live cells.        
        return (sorted(self.rowList,reverse = True)[0],\
                sorted(self.colList,reverse = True)[0])

    def clearCell(self, row, col):
        #Clears the individual cell (row, col) and sets it to dead.
        #If the cell is already dead, no action is taken.
        for item in self:
            if item == Cell(row,col):
                self.remove(item)

    def setCell(self, row, col):
        #Sets the indicated cell (row, col) to be alive.
        #If the cell is already alive, no action is taken.
        self.__add__(Cell(row,col))

    def isLiveCell(self, row, col):
        #Returns a boolean value indicating if the given
        #cell (row, col) contains a live organism.
        return Cell(row,col) in self

    def numLiveNeighbors(self, row, col):
    #checks how many live adjacents (not diagonals I think) a cell has
        surround = 0
        if self.isLiveCell(row+1,col):
            surround += 1
        if self.isLiveCell(row-1,col):
            surround += 1
        if self.isLiveCell(row, col+1):
            surround += 1
        if self.isLiveCell(row, col-1):
            surround += 1
        return surround

G = SparseLifeGrid()
G.setCell(2,3)
  • 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-13T12:23:40+00:00Added an answer on June 13, 2026 at 12:23 pm

    You need to make your Cell instances immutable, then create a __hash__ method that always remains the same. If you can’t use tuples directly, here’s an alternative (that borrows only a little from tuple):

    class Cell:
        # each cell will have exactly two values, so no need for __dict__
        __slots__ = ["row", "col"]
    
        # set up values at construction time using super().__setitem__()
        def __init__(self, row, col):
            super().__setitem__("row", row)
            super().__setitem__("col", col)
            return self
    
        # a Cell is intended to be immutable, so __setattr__ always raises an error
        def __setattr__(self, name, value):
            if hasattr(self, name):
                raise AttributeError("{!r} object attribute {!r} is read-only"
                                     .format(self.__class__.__name__, name))
            else:
                raise AttributeError("{!r} object has no attribute {!r}"
                                     .format(self.__class__.__name__, name))
    
        # comparison operators
        def __eq__(self, other):
            return (isinstance(other, Cell) and
                    self.row == other.row and
                    self.col == other.col)
    
        def __ne__(self, other):
            return not self == other
    
        # hash function, with a value borrowed from a tuple
        def __hash__(self):
            return hash((self.row, self.col))
    

    This is rather a lot of work though, for something that is equivalent to:

    Cell = collections.namedtuple(["row", "col"])
    

    Your grid class also has some issues. For instance, you’re overriding __add__, which is used for implementing the addition operator, but you don’t return anything, so it won’t work like it would be expected to. I suspect you mean to be overriding the add method (with no underscores) instead. However, if that’s the case, you’ll want to be sure you call super().add() with appropriate arguments if you want your grid to actually function as a set.

    Also, min(lst) should be much faster than sorted(lst)[0] (O(N) rather than O(N log N)).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I'm trying to create an if statement in PHP that prevents a single post
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I am trying to loop through a bunch of documents I have to put

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.