如何以与name属性相同的方式打印组合的Flag [英] How to print combined Flag in the same way as name property
本文介绍了如何以与name属性相同的方式打印组合的Flag的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Python中,可以使用Flag类表示值的组合。
In Python, you can use the Flag class to represent combinations of values.
class Color(Flag):
Red = auto()
Green = auto()
Blue = auto()
White = Red | Green | Blue
这些隐式转换为字符串,以便您可以打印它们。
These implicitly convert to strings so you can print them.
>>> print(Color.Red, Color.White, Color.Red|Color.Green)
Color.Red Color.White Color.Green|Red
name
属性为您提供了更好的打印方式,但不适用于未命名的组合值。
The name
property gives you can even nicer way to print, but it doesn't work for unnamed combined values.
>>> print(Color.Red.name, Color.White.name, (Color.Red|Color.Green).name)
Red White None
有没有办法以类似于 name
的方式获得组合的Flag值进行打印,而无需编写单独的函数?
Is there any way to get a combined Flag value to print in a similar way to name
, without writing a separate function?
例如
Color.Red | Color.Green => Red Green
推荐答案
不幸的是,没有。但这听起来像是良好的增强请求。
Unfortunately, no. But this sounds like a good enhancement request.
这篇关于如何以与name属性相同的方式打印组合的Flag的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文