Python相当于C#Enum.HasFlag [英] Python equivalent of C# Enum.HasFlag

查看:103
本文介绍了Python相当于C#Enum.HasFlag的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说在python

StateA = 1
StateB = 2
StateC = StateA | StateB

...
instance.state = StateA

在C#中有一个在Enum中的HasFlag函数,它告诉我一个对象的标志是StateC的一部分

in C# there is a HasFlag function in Enum, which tells me an object's flag is part of StateC

python中是否有等效版本?

Is there a equivalent version in python?

现在我只能这样认为(按照这样的方式),甚至不知道是否应该工作:

Right now I can only think (as bitwise noob) of this and not even sure if it supposed to work:

if instance.state | StateC == StateC:
    # yes in StateC


推荐答案

p>测试标志:

value & flag == flag

添加标志:

value |= flag

清除标志: p>

Clearing a flag:

value &= ~flag

这篇关于Python相当于C#Enum.HasFlag的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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