如何在Python中实现类似Java的枚举?

Mis*_*sty 1 python java enums

说这个的python版本是什么..这是在java中

public static enum Operations {Add, Subtract, Multiply, Divide, None};
Run Code Online (Sandbox Code Playgroud)

我正在将整个程序转换为python,我只是无法弄清楚这一部分

我全班同学

    import java.util.*;

public class Expression
{
    public static enum Operations {Add, Subtract, Multiply, Divide, None};
    int a;
    int b;
    Expression.Operations op;
    public Expression()
    {
        a = 0;
        b = 0;
        op = Expression.Operations.None;
    }
    public Expression(int value1, int value2, Expression.Operations operation)
    {
        a = value1;
        b = value2;
        op = operation;
    }

    public boolean parseString(String expressionString, Map<Character, Integer> vars)
    {
        Scanner scanner = new Scanner(expressionString);

        //Attempt to read the first value.
        if (scanner.hasNextInt())
            a = scanner.nextInt();
        else if (scanner.hasNext())
        {
            String var = scanner.next();
            //Ensure that the variable identifier is a single alphabetical character in length.
            if (!var.matches("[A-Z]{1}"))
            {
                return false;
            }
            if (vars.containsKey(var.charAt(0)))
                a = vars.get(var.charAt(0));
            else
            {
                System.err.println("ERROR: Uninitialized variable.");
                return false;
            }
        }
        else return false;

        //If more tokens exist, attempt to read the operator.
        if (scanner.hasNext())
        {
            String operator = scanner.next();
            if (operator.equals("+"))
                op = Expression.Operations.Add;
            else if (operator.equals("-"))
                op = Expression.Operations.Subtract;
            else if (operator.equals("*"))
                op = Expression.Operations.Multiply;
            else if (operator.equals("/"))
                op = Expression.Operations.Divide;
            else
                return false;

            //Attempt to read the second value.
            if (scanner.hasNextInt())
                b = scanner.nextInt();
            else if (scanner.hasNext())
            {
                String var = scanner.next();
                //Ensure that the variable identifier is a single alphabetical character in length.
                if (!var.matches("[A-Z]{1}"))
                {
                    return false;
                }
                b = vars.get(var.charAt(0));
            }
            else return false;
        }

        return true;
    }
    public int evaluate()
    {
        int value = 0;
        if (op == Expression.Operations.Add)
            value = a + b;
        if (op == Expression.Operations.Subtract)
            value = a - b;
        if (op == Expression.Operations.Multiply)
            value = a * b;
        if (op == Expression.Operations.Divide)
            value = a / b;
        if (op == Expression.Operations.None)
            value = a;
        return value;
    }
}
Run Code Online (Sandbox Code Playgroud)

JBe*_*rdo 7

您始终可以使用NamedTuple

>>> import collections
>>> Enum = collections.namedtuple('Enum','Add Subtract Multiply Divide None_')
>>> Enum(*range(1,6))
Enum(Add=1, Subtract=2, Multiply=3, Divide=4, None_=5)
>>> operations = _
>>> operations.Add
1
Run Code Online (Sandbox Code Playgroud)

在较新的Python版本上,您无法分配None,因此我将其更改为None_.


Jon*_*erg 7

Python没有枚举类.它只是使用正常的整数.使模板成为类的一部分的最简单方法是执行以下操作:

class Operation:
    ADD, SUBTRACT, MULTIPLY, DIVIDE, NONE = range(5)
Run Code Online (Sandbox Code Playgroud)

这将是分配加0和无分配4.这是最干净的方式(它将保证你没有这个序列中任何相同数字的枚举,你没有错过分配的东西其中一个数字.


小智 5

在Python中,除非在其名称的开头加上下划线,否则任何属性或方法都被视为公共.这是Python 2.7教程中的相关部分.

Python没有完全复制函数的方法static,但是您在类中定义的任何属性都将以与static变量相同的方式在实例中可见.就attribute = value在你的班级定义里面,你很好.

你不能constant在Python中创建值,但约定是UPPERCASE_IDENTIFIERS用来表示意图.

枚举不存在.在Python中,普通字符串常量通常用于此目的.刚及格"add" "subtract","multiply","divide"或者None给你的函数.

例如,在您的解析器中

if (operator.equals("+"))
    op = Expression.Operations.Add;
Run Code Online (Sandbox Code Playgroud)

会成为

if operator == "+":
    op = "add"
Run Code Online (Sandbox Code Playgroud)

在你的评估员

if (op == Expression.Operations.Add)
    value = a + b;
Run Code Online (Sandbox Code Playgroud)

会成为

if op == "add"
    value = a + b
Run Code Online (Sandbox Code Playgroud)