How to print combined Flag in the same way as name property

送分小仙女□ 提交于 2020-03-23 07:16:42

问题


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

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

Is there any way to get a combined Flag value to print in a similar way to name, without writing a separate function?

e.g.

Color.Red | Color.Green  =>  Red Green

回答1:


Unfortunately, no. But this sounds like a good enhancement request.



来源:https://stackoverflow.com/questions/60797091/how-to-print-combined-flag-in-the-same-way-as-name-property

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!