枚举功能? [英] Enum with functions?

查看:122
本文介绍了枚举功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到页面:

我熟悉,用于在Python中创建枚举。但是,我找不到具有函数的枚举的任何一个例子。

I'm familiar with this SO question for creating enums in Python. However, I can't find an example anywhere of an enum that has functions.

我主要是一个Java程序员。
我用Java编写了这个代码:

I'm mainly a Java programmer. I wrote this code in Java:

public enum Role {
    SOLDIER(4),
    DEMOMAN(2),
    SCOUT(4),
    MEDIC(2);

    private final int maxPlayers;

    private Role(int maxPlayers) {
        this.maxPlayers = maxPlayers;
    }

    public int getMaxPlayers() {  return maxPlayers; }
}

我尝试在Python中执行相同操作:

I tried to do the same in Python:

class Klass:
    SCOUT = 1
    SOLDIER = 2
    DEMOMAN = 3
    MEDIC = 4

    @staticmethod
    def maxPlayers(klass):
        return {
            Klass.SCOUT : 4,
            Klass.SOLDIER : 4,
            Klass.DEMOMAN : 2,
            Klass.MEDIC : 2,
        }[klass]



<由于某种原因我觉得我做错了
将函数与Python中的枚举相关联的最佳做法是什么?

For some reason. I feel like I'm doing it wrong. What is the best practice for associating functions to enums in Python?

我实际上并不在意,建议的答案是否不使用枚举;我只是想了解在Python中实现上述Java代码的最佳做法。

I don't actually care if the suggested answer doesn't use an enum; I'm just trying to understand the best practice for implementing the above Java code in Python.

我愿意在以下内容中使用此枚举:

I'm willing to use this enum in the following:

class Players(dict):
    def countKlass(self, klass):
        count = len(filter(lambda x: x == klass, self.values()))
        return count


推荐答案

让我们记住Java中的枚举是真的 - static final 类。他们真正的只是命名常量。

Let's remember what enums in Java really are -- static final instances of a class. All they really are is just named constants.

通常,python有利于比Java更松散(或者更喜欢,更灵活)的编码风格。其中一部分是没有一个常数的东西,其中一部分是相信你的代码的用户不要做疯狂的事情。通过这样做,您可以获得与您的java代码相似的效果:

Generally, python favors a looser (or if you prefer, more flexible) coding style than Java. Part of that is that there's no such thing as a constant, and part of it is trusting users of your code not to do crazy things. In that vein, you can get a similar effect to your java code by doing this:

class Role(object):
    class RoleType(object):
        def __init__(self, maxPlayers):
            self.maxPlayers = maxPlayers

    SOLDIER = RoleType(4)
    DEMOMAN = RoleType(2)
    SCOUT = RoleType(4)
    MEDIC = RoleType(2)

然后你可以这样访问它们

Then you can access them like this

s = Role.SOLDIER
print s.maxPlayers  # Prints 4

这个做的是阻止您的库的用户创建新的角色。这样做有点尴尬,但这应该是用户提示他们做错了。

What this doesn't do is prevent users of your library from creating new roles. Doing so is slightly awkward, though, which should be a hint to the user that "they're doing it wrong".

newRole = Role.RoleType(22)  # as an example

如果你走这条路线,或以下必须与此同住。如果你喜欢使用惯例,这通常不会是一个问题。他们可以随时访问您定义的值。

If you go this route, you just more or less have to live with that. If you favor using convention though, this generally won't be a problem. They can always just access the values you've defined.

这篇关于枚举功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆