| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 9852cb3 commit 7aaeb2a
8 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -197,7 +197,7 @@ Having two enum members with the same name is invalid:: | |||
| 197 | 197 | ... | |
| 198 | 198 | Traceback (most recent call last): | |
| 199 | 199 | ... | |
| 200 | - TypeError: Attempted to reuse key: 'SQUARE' | ||
| 200 | + TypeError: 'SQUARE' already defined as: 2 | ||
| 201 | 201 | ||
| 202 | 202 | However, two enum members are allowed to have the same value. Given two members | |
| 203 | 203 | A and B with the same value (and A defined first), B is an alias to A. By-value | |
@@ -422,7 +422,7 @@ any members. So this is forbidden:: | |||
| 422 | 422 | ... | |
| 423 | 423 | Traceback (most recent call last): | |
| 424 | 424 | ... | |
| 425 | - TypeError: Cannot extend enumerations | ||
| 425 | + TypeError: MoreColor: cannot extend enumeration 'Color' | ||
| 426 | 426 | ||
| 427 | 427 | But this is allowed:: | |
| 428 | 428 | ||
@@ -617,6 +617,7 @@ by extension, string enumerations of different types can also be compared | |||
| 617 | 617 | to each other. :class:`StrEnum` exists to help avoid the problem of getting | |
| 618 | 618 | an incorrect member:: | |
| 619 | 619 | ||
| 620 | + >>> from enum import StrEnum | ||
| 620 | 621 | >>> class Directions(StrEnum): | |
| 621 | 622 | ... NORTH = 'north', # notice the trailing comma | |
| 622 | 623 | ... SOUTH = 'south' | |
@@ -638,12 +639,22 @@ IntFlag | |||
| 638 | 639 | The next variation of :class:`Enum` provided, :class:`IntFlag`, is also based | |
| 639 | 640 | on :class:`int`. The difference being :class:`IntFlag` members can be combined | |
| 640 | 641 | using the bitwise operators (&, \|, ^, ~) and the result is still an | |
| 641 | - :class:`IntFlag` member. However, as the name implies, :class:`IntFlag` | ||
| 642 | + :class:`IntFlag` member, if possible. However, as the name implies, :class:`IntFlag` | ||
| 642 | 643 | members also subclass :class:`int` and can be used wherever an :class:`int` is | |
| 643 | - used. Any operation on an :class:`IntFlag` member besides the bit-wise | ||
| 644 | - operations will lose the :class:`IntFlag` membership. | ||
| 644 | + used. | ||
| 645 | + | ||
| 646 | + .. note:: | ||
| 647 | + | ||
| 648 | + Any operation on an :class:`IntFlag` member besides the bit-wise operations will | ||
| 649 | + lose the :class:`IntFlag` membership. | ||
| 650 | + | ||
| 651 | + .. note:: | ||
| 652 | + | ||
| 653 | + Bit-wise operations that result in invalid :class:`IntFlag` values will lose the | ||
| 654 | + :class:`IntFlag` membership. | ||
| 645 | 655 | ||
| 646 | 656 | .. versionadded:: 3.6 | |
| 657 | + .. versionchanged:: 3.10 | ||
| 647 | 658 | ||
| 648 | 659 | Sample :class:`IntFlag` class:: | |
| 649 | 660 | ||
@@ -671,21 +682,41 @@ It is also possible to name the combinations:: | |||
| 671 | 682 | >>> Perm.RWX | |
| 672 | 683 | <Perm.RWX: 7> | |
| 673 | 684 | >>> ~Perm.RWX | |
| 674 | - <Perm.-8: -8> | ||
| 685 | + <Perm: 0> | ||
| 686 | + >>> Perm(7) | ||
| 687 | + <Perm.RWX: 7> | ||
| 688 | + | ||
| 689 | + .. note:: | ||
| 690 | + | ||
| 691 | + Named combinations are considered aliases. Aliases do not show up during | ||
| 692 | + iteration, but can be returned from by-value lookups. | ||
| 693 | + | ||
| 694 | + .. versionchanged:: 3.10 | ||
| 675 | 695 | ||
| 676 | 696 | Another important difference between :class:`IntFlag` and :class:`Enum` is that | |
| 677 | 697 | if no flags are set (the value is 0), its boolean evaluation is :data:`False`:: | |
| 678 | 698 | ||
| 679 | 699 | >>> Perm.R & Perm.X | |
| 680 | - <Perm.0: 0> | ||
| 700 | + <Perm: 0> | ||
| 681 | 701 | >>> bool(Perm.R & Perm.X) | |
| 682 | 702 | False | |
| 683 | 703 | ||
| 684 | 704 | Because :class:`IntFlag` members are also subclasses of :class:`int` they can | |
| 685 | - be combined with them:: | ||
| 705 | + be combined with them (but may lose :class:`IntFlag` membership:: | ||
| 706 | + | ||
| 707 | + >>> Perm.X | 4 | ||
| 708 | + <Perm.R|X: 5> | ||
| 686 | 709 | ||
| 687 | 710 | >>> Perm.X | 8 | |
| 688 | - <Perm.8|X: 9> | ||
| 711 | + 9 | ||
| 712 | + | ||
| 713 | + .. note:: | ||
| 714 | + | ||
| 715 | + The negation operator, ``~``, always returns an :class:`IntFlag` member with a | ||
| 716 | + positive value:: | ||
| 717 | + | ||
| 718 | + >>> (~Perm.X).value == (Perm.R|Perm.W).value == 6 | ||
| 719 | + True | ||
| 689 | 720 | ||
| 690 | 721 | :class:`IntFlag` members can also be iterated over:: | |
| 691 | 722 | ||
@@ -717,7 +748,7 @@ flags being set, the boolean evaluation is :data:`False`:: | |||
| 717 | 748 | ... GREEN = auto() | |
| 718 | 749 | ... | |
| 719 | 750 | >>> Color.RED & Color.GREEN | |
| 720 | - <Color.0: 0> | ||
| 751 | + <Color: 0> | ||
| 721 | 752 | >>> bool(Color.RED & Color.GREEN) | |
| 722 | 753 | False | |
| 723 | 754 | ||
@@ -751,7 +782,7 @@ value:: | |||
| 751 | 782 | ||
| 752 | 783 | >>> purple = Color.RED | Color.BLUE | |
| 753 | 784 | >>> list(purple) | |
| 754 | - [<Color.BLUE: 2>, <Color.RED: 1>] | ||
| 785 | + [<Color.RED: 1>, <Color.BLUE: 2>] | ||
| 755 | 786 | ||
| 756 | 787 | .. versionadded:: 3.10 | |
| 757 | 788 | ||
@@ -953,7 +984,7 @@ to handle any extra arguments:: | |||
| 953 | 984 | ... BLEACHED_CORAL = () # New color, no Pantone code yet! | |
| 954 | 985 | ... | |
| 955 | 986 | >>> Swatch.SEA_GREEN | |
| 956 | - <Swatch.SEA_GREEN: 2> | ||
| 987 | + <Swatch.SEA_GREEN> | ||
| 957 | 988 | >>> Swatch.SEA_GREEN.pantone | |
| 958 | 989 | '1246' | |
| 959 | 990 | >>> Swatch.BLEACHED_CORAL.pantone | |
@@ -1144,6 +1175,14 @@ Supported ``_sunder_`` names | |||
| 1144 | 1175 | :class:`auto` to get an appropriate value for an enum member; may be | |
| 1145 | 1176 | overridden | |
| 1146 | 1177 | ||
| 1178 | + .. note:: | ||
| 1179 | + | ||
| 1180 | + For standard :class:`Enum` classes the next value chosen is the last value seen | ||
| 1181 | + incremented by one. | ||
| 1182 | + | ||
| 1183 | + For :class:`Flag`-type classes the next value chosen will be the next highest | ||
| 1184 | + power-of-two, regardless of the last value seen. | ||
| 1185 | + | ||
| 1147 | 1186 | .. versionadded:: 3.6 ``_missing_``, ``_order_``, ``_generate_next_value_`` | |
| 1148 | 1187 | .. versionadded:: 3.7 ``_ignore_`` | |
| 1149 | 1188 | ||
@@ -1159,7 +1198,9 @@ and raise an error if the two do not match:: | |||
| 1159 | 1198 | ... | |
| 1160 | 1199 | Traceback (most recent call last): | |
| 1161 | 1200 | ... | |
| 1162 | - TypeError: member order does not match _order_ | ||
| 1201 | + TypeError: member order does not match _order_: | ||
| 1202 | + ['RED', 'BLUE', 'GREEN'] | ||
| 1203 | + ['RED', 'GREEN', 'BLUE'] | ||
| 1163 | 1204 | ||
| 1164 | 1205 | .. note:: | |
| 1165 | 1206 | ||
@@ -1179,23 +1220,22 @@ Private names are not converted to Enum members, but remain normal attributes. | |||
| 1179 | 1220 | """""""""""""""""""" | |
| 1180 | 1221 | ||
| 1181 | 1222 | :class:`Enum` members are instances of their :class:`Enum` class, and are | |
| 1182 | - normally accessed as ``EnumClass.member``. Under certain circumstances they | ||
| 1183 | - can also be accessed as ``EnumClass.member.member``, but you should never do | ||
| 1184 | - this as that lookup may fail or, worse, return something besides the | ||
| 1185 | - :class:`Enum` member you are looking for (this is another good reason to use | ||
| 1186 | - all-uppercase names for members):: | ||
| 1223 | + normally accessed as ``EnumClass.member``. In Python versions ``3.5`` to | ||
| 1224 | + ``3.9`` you could access members from other members -- this practice was | ||
| 1225 | + discouraged, and in ``3.10`` :class:`Enum` has returned to not allowing it:: | ||
| 1187 | 1226 | ||
| 1188 | 1227 | >>> class FieldTypes(Enum): | |
| 1189 | 1228 | ... name = 0 | |
| 1190 | 1229 | ... value = 1 | |
| 1191 | 1230 | ... size = 2 | |
| 1192 | 1231 | ... | |
| 1193 | 1232 | >>> FieldTypes.value.size | |
| 1194 | - <FieldTypes.size: 2> | ||
| 1195 | - >>> FieldTypes.size.value | ||
| 1196 | - 2 | ||
| 1233 | + Traceback (most recent call last): | ||
| 1234 | + ... | ||
| 1235 | + AttributeError: FieldTypes: no attribute 'size' | ||
| 1197 | 1236 | ||
| 1198 | 1237 | .. versionchanged:: 3.5 | |
| 1238 | + .. versionchanged:: 3.10 | ||
| 1199 | 1239 | ||
| 1200 | 1240 | ||
| 1201 | 1241 | Creating members that are mixed with other data types | |
@@ -1237,14 +1277,14 @@ but not of the class:: | |||
| 1237 | 1277 | >>> dir(Planet) | |
| 1238 | 1278 | ['EARTH', 'JUPITER', 'MARS', 'MERCURY', 'NEPTUNE', 'SATURN', 'URANUS', 'VENUS', '__class__', '__doc__', '__members__', '__module__'] | |
| 1239 | 1279 | >>> dir(Planet.EARTH) | |
| 1240 | - ['__class__', '__doc__', '__module__', 'name', 'surface_gravity', 'value'] | ||
| 1280 | + ['__class__', '__doc__', '__module__', 'mass', 'name', 'radius', 'surface_gravity', 'value'] | ||
| 1241 | 1281 | ||
| 1242 | 1282 | ||
| 1243 | 1283 | Combining members of ``Flag`` | |
| 1244 | 1284 | """"""""""""""""""""""""""""" | |
| 1245 | 1285 | ||
| 1246 | - If a combination of Flag members is not named, the :func:`repr` will include | ||
| 1247 | - all named flags and all named combinations of flags that are in the value:: | ||
| 1286 | + Iterating over a combination of Flag members will only return the members that | ||
| 1287 | + are comprised of a single bit:: | ||
| 1248 | 1288 | ||
| 1249 | 1289 | >>> class Color(Flag): | |
| 1250 | 1290 | ... RED = auto() | |
@@ -1254,10 +1294,10 @@ all named flags and all named combinations of flags that are in the value:: | |||
| 1254 | 1294 | ... YELLOW = RED | GREEN | |
| 1255 | 1295 | ... CYAN = GREEN | BLUE | |
| 1256 | 1296 | ... | |
| 1257 | - >>> Color(3) # named combination | ||
| 1297 | + >>> Color(3) | ||
| 1258 | 1298 | <Color.YELLOW: 3> | |
| 1259 | - >>> Color(7) # not named combination | ||
| 1260 | - <Color.CYAN|MAGENTA|BLUE|YELLOW|GREEN|RED: 7> | ||
| 1299 | + >>> Color(7) | ||
| 1300 | + <Color.RED|GREEN|BLUE: 7> | ||
| 1261 | 1301 | ||
| 1262 | 1302 | ``StrEnum`` and :meth:`str.__str__` | |
| 1263 | 1303 | """"""""""""""""""""""""""""""""""" | |
@@ -1269,3 +1309,71 @@ parts of Python will read the string data directly, while others will call | |||
| 1269 | 1309 | :meth:`StrEnum.__str__` will be the same as :meth:`str.__str__` so that | |
| 1270 | 1310 | ``str(StrEnum.member) == StrEnum.member`` is true. | |
| 1271 | 1311 | ||
| 1312 | + ``Flag`` and ``IntFlag`` minutia | ||
| 1313 | + """""""""""""""""""""""""""""""" | ||
| 1314 | + | ||
| 1315 | + The code sample:: | ||
| 1316 | + | ||
| 1317 | + >>> class Color(IntFlag): | ||
| 1318 | + ... BLACK = 0 | ||
| 1319 | + ... RED = 1 | ||
| 1320 | + ... GREEN = 2 | ||
| 1321 | + ... BLUE = 4 | ||
| 1322 | + ... PURPLE = RED | BLUE | ||
| 1323 | + ... WHITE = RED | GREEN | BLUE | ||
| 1324 | + ... | ||
| 1325 | + | ||
| 1326 | + - single-bit flags are canonical | ||
| 1327 | + - multi-bit and zero-bit flags are aliases | ||
| 1328 | + - only canonical flags are returned during iteration:: | ||
| 1329 | + | ||
| 1330 | + >>> list(Color.WHITE) | ||
| 1331 | + [<Color.RED: 1>, <Color.GREEN: 2>, <Color.BLUE: 4>] | ||
| 1332 | + | ||
| 1333 | + - negating a flag or flag set returns a new flag/flag set with the | ||
| 1334 | + corresponding positive integer value:: | ||
| 1335 | + | ||
| 1336 | + >>> Color.GREEN | ||
| 1337 | + <Color.GREEN: 2> | ||
| 1338 | + | ||
| 1339 | + >>> ~Color.GREEN | ||
| 1340 | + <Color.PURPLE: 5> | ||
| 1341 | + | ||
| 1342 | + - names of pseudo-flags are constructed from their members' names:: | ||
| 1343 | + | ||
| 1344 | + >>> (Color.RED | Color.GREEN).name | ||
| 1345 | + 'RED|GREEN' | ||
| 1346 | + | ||
| 1347 | + - multi-bit flags, aka aliases, can be returned from operations:: | ||
| 1348 | + | ||
| 1349 | + >>> Color.RED | Color.BLUE | ||
| 1350 | + <Color.PURPLE: 5> | ||
| 1351 | + | ||
| 1352 | + >>> Color(7) # or Color(-1) | ||
| 1353 | + <Color.WHITE: 7> | ||
| 1354 | + | ||
| 1355 | + - membership / containment checking has changed slightly -- zero valued flags | ||
| 1356 | + are never considered to be contained:: | ||
| 1357 | + | ||
| 1358 | + >>> Color.BLACK in Color.WHITE | ||
| 1359 | + False | ||
| 1360 | + | ||
| 1361 | + otherwise, if all bits of one flag are in the other flag, True is returned:: | ||
| 1362 | + | ||
| 1363 | + >>> Color.PURPLE in Color.WHITE | ||
| 1364 | + True | ||
| 1365 | + | ||
| 1366 | + There is a new boundary mechanism that controls how out-of-range / invalid | ||
| 1367 | + bits are handled: ``STRICT``, ``CONFORM``, ``EJECT`', and ``KEEP``: | ||
| 1368 | + | ||
| 1369 | + * STRICT --> raises an exception when presented with invalid values | ||
| 1370 | + * CONFORM --> discards any invalid bits | ||
| 1371 | + * EJECT --> lose Flag status and become a normal int with the given value | ||
| 1372 | + * KEEP --> keep the extra bits | ||
| 1373 | + - keeps Flag status and extra bits | ||
| 1374 | + - extra bits do not show up in iteration | ||
| 1375 | + - extra bits do show up in repr() and str() | ||
| 1376 | + | ||
| 1377 | + The default for Flag is ``STRICT``, the default for ``IntFlag`` is ``DISCARD``, | ||
| 1378 | + and the default for ``_convert_`` is ``KEEP`` (see ``ssl.Options`` for an | ||
| 1379 | + example of when ``KEEP`` is needed). | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments