枚举在python [英] Enumerations in python
本文介绍了枚举在python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在python中枚举的方法是什么?
Whats the recognised way of doing enumerations in python?
例如,我正在写一个游戏,希望能够上,下,左和右。我正在使用字符串,因为我还没有弄清楚python中的枚举是如何工作的,所以我的逻辑很像这样:
For example, at the moment I'm writing a game and want to be able to move "up", "down", "left" and "right". I'm using strings because I haven't yet figured out how enumerations work in python, and so my logic is littered with things like this:
def move(self, direction):
if direction == "up":
# Do something
我想用 Directions.up
替换up
/ p>
I want to replace "up"
with something like Directions.up
推荐答案
class Directions:
up = 0
down = 1
left = 2
right =3
这篇关于枚举在python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文