栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

表示Flag枚举中的所有值

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

表示Flag枚举中的所有值

有几种方法可以解决此问题:

  • 使用

    classproperty
    (请参阅
    Zero's answer

  • 使用类装饰器(请参阅参考资料

    MSeifert's answer

  • 使用mixin(

    currently buggy

  • 创建一个新的基类(见下文)


使用类属性方法要注意的一件事是,因为描述符是在类而不是元类上定义的,所以缺少针对设置和删除的常规保护措施-换句话说:

>>> RefreshFlags.ALL<RefreshFlags.DEFENSES|BUILDINGS|RESOURCES|EVENTS: 15>>>> RefreshFlags.ALL = 'oops'>>> RefreshFlags.ALL'oops'

创建一个新的基类:

# lightly testedfrom enum import Flag, autofrom operator import or_ as _or_from functools import reduceclass AllFlag(Flag):    @classproperty    def ALL(cls):        cls_name = cls.__name__        if not len(cls): raise AttributeError('empty %s does not have an ALL value' % cls_name)        value = cls(reduce(_or_, cls))        cls._member_map_['ALL'] = value        return value

并在使用中:

class RefreshFlag(AllFlag):    EVENTS = auto()    RESOURCES = auto()    BUILDINGS = auto()    DEFENSES = auto()>>> RefreshFlag.ALL<RefreshFlag.DEFENSES|BUILDINGS|RESOURCES|EVENTS: 15>

ALL
属性中有趣的区别是名称的设置
_member_map_
-这允许为Enum成员提供相同的保护:

>>> RefreshFlag.ALL = 9Traceback (most recent call last):  ....AttributeError: Cannot reassign members.

但是,这里有一个竞争条件:如果在第一次激活 之前

RefreshFlag.ALL = ...
发生
RefreshFlag.ALL
那么它将被破坏;因此,在这种情况下,我将使用装饰器,因为装饰器将在处理Enum之前对其进行处理。

# lightly testedfrom enum import Flag, autofrom operator import or_ as _or_from functools import reducedef with_limits(enumeration):    "add NONE and ALL psuedo-members to enumeration"    none_mbr = enumeration(0)    all_mbr = enumeration(reduce(_or_, enumeration))    enumeration._member_map_['NONE'] = none_mbr    enumeration._member_map_['ALL'] = all_mbr    return enumeration

并在使用中:

@with_limitsclass RefreshFlag(Flag):    EVENTS = auto()    RESOURCES = auto()    BUILDINGS = auto()    DEFENSES = auto()>>> RefreshFlag.ALL = 99Traceback (most recent call last):  ...AttributeError: Cannot reassign members.>>> RefreshFlag.ALL <RefreshFlag.DEFENSES|BUILDINGS|RESOURCES|EVENTS: 15>>>> RefreshFlag.NONE<RefreshFlag.0: 0>


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/661360.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号