Flask接口签名sign原理与实例代码浅析
561
2022-08-29
python枚举之Enum模块(枚举类型enum用法)
枚举是与多个唯一常量值绑定的一组符号(即成员)。枚举中的成员可以进行身份比较,并且枚举自身也可迭代。枚举成员名称建议使用大写字母# 示例from enum import Enum,unique, IntEnum(支持成员比较)@unique # unique装饰器确保属性名和值唯一class Color(Enum): RED = 1 GREEN = 2 BLUE = 3 # YELLOW = 2 会报错// 不以类形式构造枚举BugStatus = enum.Enum( value='BugStatus', names=[ ('new', 3) ('old', 2) ('won_fix', 1) ],)for status in BugStatus: print("{0}:{1}".format(status.name, status.value))// 非整数成员值,元组import enumclass BugStatus(enum.Enum): new = (7, ['incomplete', 'invalid', 'won_fix', 'in_progress']) incomplete = (6, ['new', 'wond_fix']) invalid = (5, ['new']) wont_fix = (4, ['new']) in_progress = (3, ['new', 'fix_committed']) fix_committed = (2, ['in_progress', 'fix_released']) fix_released = (1, ['new']) def __init__(self, num: int, transitions: list) -> None: self.num = num self.transitions = transitions def can_transition(self, new_state: object): print(new_state.name) print(self.transitions) return new_state.name in self.transitions# 如果成员的值是一个元组,那么成员会被作为单个参数被传递到__init__()print('Name', BugStatus.in_progress)print('Value', BugStatus.in_progress.value) # (3, ['new', 'fix_committed'])print('Custom attribute:', BugStatus.in_progress.transitions) # ['new', 'fix_committed']print('Using attribute:', BugStatus.in_progress.can_transition(BugStatus.new)) #True# 使用字典import enumclass BugStatus(enum.Enum): new = { 'num': 7, 'transitions': [ 'incomplete', 'invalid', 'wont_fix', 'in_progress', ], } incomplete = {'num': 6, 'transitions': ['new']} # 复杂成员的值会被作为唯一参数传递__init__() def __init__(self, vals: dict) -> None: self.num = vals['num'] self.transitions = vals['transitions'] def can_transition(self, new_state: object): return new_state.name in self.transitions
个性签名:代码过万,键盘敲烂!!!
如果觉得这篇文章对你有小小的帮助的话,记得“推荐”哦,博主在此感谢!
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~