Skip to content
Advertisement

I’ve got an object that is basically an int with some extra properties. Can I coerce it into an int when it’s used as a list index?

Context: I’m making a game that happens in a maze made of square tiles and almost everything, from movement to attacks, involves directions, which are mostly used to index lists. Subtracting or adding to directions is an easy way to turn left or right, but I always have to check that they are still within bounds, and I would like to automate that by making a custom class.

Here is how I’m currently doing it:

JavaScript

And here is what I’d like to do:

JavaScript

The only problem with that is that I am using UP, RIGHT, etc. as indexes, like I have a Tile that has a list of four Wall and I constantly call Tile.walls[direction] with direction being one of my four constants, and I don’t want to have to specify Tile.walls[int(direction)] everytime.

Is there a way to have direction automatically coerced into an int whenever it’s used for indexing?

Advertisement

Answer

Yes, simply define __index__(). For example:

JavaScript

This also makes __int__ unnecessary since __index__ is used as a fallback.

JavaScript

P.S. For this answer, I’m ignoring any design considerations. Using an IntEnum might be a better solution, I’m not sure.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement