Browse Source

Enum

pull/10/head
Jure Šorn 6 years ago
parent
commit
a524c022c0
1 changed files with 6 additions and 7 deletions
  1. 13
      README.md

13
README.md

@ -677,14 +677,13 @@ random_member = random.choice(list(<enum>))
Cutlery = Enum('Cutlery', ['knife', 'fork', 'spoon'])
Cutlery = Enum('Cutlery', 'knife fork spoon')
Cutlery = Enum('Cutlery', {'knife': 1, 'fork': 2, 'spoon': 3})
```
# Functions can not be values, so they must be enclosed in tuple:
LogicOp = Enum('LogicOp', {'AND': (lambda l, r: l and r, ),
'OR' : (lambda l, r: l or r, )})
# But 'list(<enum>)' will only work if there is another value in the tuple:
LogicOp = Enum('LogicOp', {'AND': (auto(), lambda l, r: l and r),
'OR' : (auto(), lambda l, r: l or r)})
```python
# Functions can not be values, unless they are wrapped:
from functools import partial
LogicOp = Enum('LogicOp', {'AND': partial(lambda l, r: l and r),
'OR' : partial(lambda l, r: l or r)})
```

Loading…
Cancel
Save