File tree Expand file tree Collapse file tree 2 files changed +27
-0
lines changed Expand file tree Collapse file tree 2 files changed +27
-0
lines changed Original file line number Diff line number Diff line change @@ -888,6 +888,32 @@ Using an auto-numbering :meth:`__new__` would look like::
888
888
>>> Color.GREEN.value
889
889
2
890
890
891
+ To make a more general purpose ``AutoNumber ``, add ``*args `` to the signature::
892
+
893
+ >>> class AutoNumber(NoValue):
894
+ ... def __new__(cls, *args): # this is the only change from above
895
+ ... value = len(cls.__members__) + 1
896
+ ... obj = object.__new__(cls)
897
+ ... obj._value_ = value
898
+ ... return obj
899
+ ...
900
+
901
+ Then when you inherit from ``AutoNumber `` you can write your own ``__init__ ``
902
+ to handle any extra arguments::
903
+
904
+ >>> class Swatch(AutoNumber):
905
+ ... def __init__(self, pantone='unknown'):
906
+ ... self.pantone = pantone
907
+ ... AUBURN = '3497'
908
+ ... SEA_GREEN = '1246'
909
+ ... BLEACHED_CORAL = () # New color, no Pantone code yet!
910
+ ...
911
+ >>> Swatch.SEA_GREEN
912
+ <Swatch.SEA_GREEN: 2>
913
+ >>> Swatch.SEA_GREEN.pantone
914
+ '1246'
915
+ >>> Swatch.BLEACHED_CORAL.pantone
916
+ 'unknown'
891
917
892
918
.. note ::
893
919
Original file line number Diff line number Diff line change @@ -1678,6 +1678,7 @@ Févry Thibault
1678
1678
Lowe Thiderman
1679
1679
Nicolas M. Thiéry
1680
1680
James Thomas
1681
+ Reuben Thomas
1681
1682
Robin Thomas
1682
1683
Brian Thorne
1683
1684
Christopher Thorne
You can’t perform that action at this time.
0 commit comments