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