TartanLlama TartanLlama - 26 days ago 6
Python Question

Python User-Defined Data Type

I am writing a rogue-like game in Python and am defining my

Tile
class. A tile can either be blocked, wall or floor. I would like to be able to write something along the lines of

self.state = Blocked


similar to how you would use a boolean (but with three values).

Is there a nice way for me to define a data type to enable me to do this?

Thanks

Answer

For three constants, I would use the unpacking version of the enum 'pattern':

Blocked, Wall, Floor = range(3)

If it gets more complex than that though, I would have a look at other enum types in python.