26.04.13 11:00, Greg Ewing написав(ла): > However, there's a worse problem with defining enum > inheritance that way. The subtype relation for extensible > enums works the opposite way to that of classes. > > To see this, imagine a function expecting something > of type Colors. It knows what to do with red, green and > blue, but not anything else. So you *can't* pass it > something of type MoreColors, because not all values > of type MoreColors are of type Colors. > > On the other hand, you *can* pass a value of type Colors > to something expecting MoreColors, because every value of > Colors is also in MoreColors. I propose do not use an inheritance for extending enums, but use an import. class Colors(Enum): red = 1 green = 2 blue = 3 class MoreColors(Enum): from Colors import * cyan = 4 magenta = 5 yellow = 6 An inheritance we can use to limit a type of values. class Colors(int, Enum): # only int values red = 1 green = 2 blue = 3 Colors.viridity = green
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4