gpt4 book ai didi

python - python中的枚举

转载 作者:IT老高 更新时间:2023-10-28 22:05:49 27 4
gpt4 key购买 nike

这个问题已经在这里有了答案:




11年前关闭。




Duplicate:
What’s the best way to implement an ‘enum’ in Python?



在python中进行枚举的公认方法是什么?

例如,目前我正在编写游戏,并且希望能够将“上”,“下”,“左”和“右”移动。我使用字符串是因为我还没有弄清楚枚举在python中是如何工作的,所以我的逻辑充满了这样的东西:
def move(self, direction):
if direction == "up":
# Do something

我想用 "up"代替 Directions.up

最佳答案

class Directions:
up = 0
down = 1
left = 2
right =3

关于python - python中的枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1969005/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com