我必须在 python 中编写一个以前用 Java 编写的库。来自 Java 背景的 python 给我带来了一些困难。我坚持选择正确的pythonic做事方式..
所以,我的java代码是这样的:
import java.util.Collection;
public abstract class MyEnumBaseClass
{
protected int value;
protected String description = null;
protected MyEnumBaseClass(int iValue, String iDescription)
{
value = iValue;
description = iDescription;
}
public int getValue()
{
return value;
}
public String getDescription()
{
return description;
}
protected static MyEnumBaseClass getEnum(Collection<MyEnumBaseClass> iter, int value)
{
for (MyEnumBaseClass enumObj : iter)
{
if (enumObj.getValue() == value)
{
return enumObj;
}
}
return null;
}
}
import java.util.ArrayList;
import java.util.Collection;
public class MyEnumClass extends MyEnumBaseClass
{
private final static Collection<MyEnumBaseClass> enumList = new ArrayList<MyEnumBaseClass>();
public final static int ERROR1 = 1;
public final static int ERROR2 = 2;
public final static int ERROR3 = 3;
public final static int ERROR4 = 4;
public final static MyEnumClass ErrorEnum1 = new MyEnumClass(ERROR1, "ERROR1");
public final static MyEnumClass ErrorEnum2 = new MyEnumClass(ERROR2, "ERROR1");
public final static MyEnumClass ErrorEnum3 = new MyEnumClass(ERROR3, "ERROR1");
public final static MyEnumClass ErrorEnum4 = new MyEnumClass(ERROR4, "ERROR1");
protected MyEnumClass(int iValue, String iDescription)
{
super(iValue, iDescription);
}
public static int getCount()
{
return enumList.size();
}
public static Collection<MyEnumBaseClass> getList()
{
return enumList;
}
public static MyEnumBaseClass getEnum(int value)
{
return getEnum(enumList, value);
}
}
我想用python写一些东西。我理解两种语言完全不同。我不想复制确切的代码,但我想在 python 中编写一些东西,它可以提供 Java 代码提供的功能。
所以我想出了类似的东西:
# MODULE MYENUMBASECLASS:::
class MyEnumBaseClass(object):
def __init__(self, iValue, iDescription, ui = None):
self._value = iValue
self._description = iDescription
def getValue(self):
return self._value
def getDescription(self):
return self._description
@classmethod
def getEnum(cls, value, itr):
for enumObj in itr:
if enumObj.getValue() == value:
return enumObj
return None
# MODULE: ENUMS:::
from MyEnumBaseClass import MyEnumBaseClass
__all__ = ["MyEnumClassConstants", "MyEnumClass", "MyEnums"]
_enumList = []
class MyEnumClassConstants(object):
ERROR1 = 1
ERROR2 = 2
ERROR3 = 3
ERROR4 = 4
class MyEnumClass(MyEnumBaseClass):
def __init__(self, v, d, ui):
global _enumList
super(MyEnumClass, self).__init__(v, d, ui)
_enumList.append(self)
@staticmethod
def getCount():
return len(_enumList)
@staticmethod
def getList():
return _enumList
@classmethod
def getEmum(cls, value, itr = None):
return super(MyEnumClass, cls).getEnum(value, _enumList)
class MyEnums(object):
ErrorEnum1 = MyEnumClass(MyEnumClassConstants.ERROR1, "ERROR1");
ErrorEnum2 = MyEnumClass(MyEnumClassConstants.ERROR2, "ERROR2");
ErrorEnum3 = MyEnumClass(MyEnumClassConstants.ERROR3, "ERROR3");
ErrorEnum4 = MyEnumClass(MyEnumClassConstants.ERROR4, "ERROR4");
我想知道:
这是正确的pythonic方式吗?
我想将 ErrorEnum1,2,3,4 和常量移出 MyEnums 类作为模块变量。但是这样我就会在我的所有变量中有一个很长的列表。当我在其他模块中导入 Enums 模块时,我也有变量名冲突的风险(其他一些 Enums2 模块也可能有 ErrorEnum1,2,3.. 但这不是一个大问题。我们总是可以使用 Enums.ErrorEnum1 和 Enums2 .ErrorEnum1)。我想对了吗?
我知道这并不完美(我的第一个 python 代码)。所以我邀请你们给我一些想法。
谢谢