我有一个enum国籍:
class Nationality:
Poland='PL'
Germany='DE'
France='FR'
Run Code Online (Sandbox Code Playgroud)
如何以这种或类似的方式将此枚举转换为int:
position_of_enum = int(Nationality.Poland) # here I want to get 0
Run Code Online (Sandbox Code Playgroud)
我知道如果我有代码,我可以这样做:
counter=0
for member in dir(Nationality):
if getattr(Nationality, member) == code:
lookFor = member
counter += 1
return counter
Run Code Online (Sandbox Code Playgroud)
但我没有,这种方式对于python来说太大了.我确信有一些更简单的东西.
小智 27
请使用IntEnum
from enum import IntEnum
class loggertype(IntEnum):
Info = 0
Warning = 1
Error = 2
Fetal = 3
int(loggertype.Info)
0
Run Code Online (Sandbox Code Playgroud)
Eth*_*man 14
使用enum34backport或aenum 1,
您可以创建一个专门的Enum:
# using enum34
from enum import Enum
class Nationality(Enum):
PL = 0, 'Poland'
DE = 1, 'Germany'
FR = 2, 'France'
def __new__(cls, value, name):
member = object.__new__(cls)
member._value_ = value
member.fullname = name
return member
def __int__(self):
return self.value
Run Code Online (Sandbox Code Playgroud)
并在使用中:
>>> print(Nationality.PL)
Nationality.PL
>>> print(int(Nationality.PL))
0
>>> print(Nationality.PL.fullname)
'Poland'
Run Code Online (Sandbox Code Playgroud)
使用1更容易编写上述内容:aenum
# using aenum
from aenum import Enum, MultiValue
class Nationality(Enum):
_init_ = 'value fullname'
_settings_ = MultiValue
PL = 0, 'Poland'
DE = 1, 'Germany'
FR = 2, 'France'
def __int__(self):
return self.value
Run Code Online (Sandbox Code Playgroud)
它具有以下附加功能:
>>> Nationality('Poland')
<Nationality.PL: 0>
Run Code Online (Sandbox Code Playgroud)
1披露:我是Python stdlibEnum,enum34backport和Advanced Enumeration(aenum) 库的作者.
Joe*_*ton 11
有更好的(和更多"Pythonic")方式做你想要的.
使用元组(或者如果需要修改它的列表),将保留订单:
code_lookup = ('PL', 'DE', 'FR')
return code_lookup.index('PL')
Run Code Online (Sandbox Code Playgroud)
或者使用字典:
code_lookup = {'PL':0, 'FR':2, 'DE':3}
return code_lookup['PL']
Run Code Online (Sandbox Code Playgroud)
在我看来,后者更可取,因为它更具可读性和显性性.
namedtuple在你的具体情况下,A 也可能有用,虽然它可能有点过分:
import collections
Nationalities = collections.namedtuple('Nationalities',
['Poland', 'France', 'Germany'])
nat = Nationalities('PL', 'FR', 'DE')
print nat.Poland
print nat.index(nat.Germany)
Run Code Online (Sandbox Code Playgroud)
为什么不将值定义为数字而不是字符串:
class Nationality:
POLAND = 0
GERMANY = 1
FRANCE = 2
Run Code Online (Sandbox Code Playgroud)
如果您需要访问两个字母的名称,只需提供一个映射它们的表即可。(或以其他方式映射的字典等)
实现这一目标的一种简单方法是:
from enum import Enum
class Nationality(Enum):
Poland= 1, 'PL'
Germany= 2, 'DE'
France= 3, 'FR'
def __int__(self):
return self.value[0]
def __str__(self):
return self.value[1]
# Example use
if __name__ == "__main__":
print(int(Nationality.Poland))
print(Nationality.Poland)
print(int(Nationality.Poland) == 1)
Run Code Online (Sandbox Code Playgroud)
输出:
>>> 1
>>> PL
>>> True
Run Code Online (Sandbox Code Playgroud)
说明:您可以根据需要使用所需的计数和字符串表示形式创建枚举,并通过覆盖相应的__int__和__str__方法来获得所需的功能。据我所知,这并没有破坏任何契约,enum而且我更喜欢将功能封装在各自的类中。
你不能.Python不存储类元素的顺序,dir()并将以任何顺序返回它们.
从你的评论中看到你确实需要从字符串到整数的映射,你实际上应该这样做:
code_lookup = {
'PL': ("Poland", 0),
'DE': ("Germany", 1),
'FR': ("France", 2),
...
}
Run Code Online (Sandbox Code Playgroud)
from enum import Enum
class Phone(Enum):
APPLE = 1 #do not write comma (,)
ANDROID = 2
#as int:
Phone.APPLE.value
Run Code Online (Sandbox Code Playgroud)
如果使用逗号,则需要按索引访问元组:
class Phone(Enum):
APPLE = 1, # note: there is comma (,)
ANDROID = 2,
#as int:
Phone.APPLE.value[0]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
23789 次 |
| 最近记录: |