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 @@ -925,6 +925,32 @@ Using an auto-numbering :meth:`__new__` would look like::
925
925
>>> Color.GREEN.value
926
926
2
927
927
928
+ To make a more general purpose ``AutoNumber ``, add ``*args `` to the signature::
929
+
930
+ >>> class AutoNumber(NoValue):
931
+ ... def __new__(cls, *args): # this is the only change from above
932
+ ... value = len(cls.__members__) + 1
933
+ ... obj = object.__new__(cls)
934
+ ... obj._value_ = value
935
+ ... return obj
936
+ ...
937
+
938
+ Then when you inherit from ``AutoNumber `` you can write your own ``__init__ ``
939
+ to handle any extra arguments::
940
+
941
+ >>> class Swatch(AutoNumber):
942
+ ... def __init__(self, pantone='unknown'):
943
+ ... self.pantone = pantone
944
+ ... AUBURN = '3497'
945
+ ... SEA_GREEN = '1246'
946
+ ... BLEACHED_CORAL = () # New color, no Pantone code yet!
947
+ ...
948
+ >>> Swatch.SEA_GREEN
949
+ <Swatch.SEA_GREEN: 2>
950
+ >>> Swatch.SEA_GREEN.pantone
951
+ '1246'
952
+ >>> Swatch.BLEACHED_CORAL.pantone
953
+ 'unknown'
928
954
929
955
.. note ::
930
956
Original file line number Diff line number Diff line change @@ -1723,6 +1723,7 @@ Févry Thibault
1723
1723
Lowe Thiderman
1724
1724
Nicolas M. Thiéry
1725
1725
James Thomas
1726
+ Reuben Thomas
1726
1727
Robin Thomas
1727
1728
Brian Thorne
1728
1729
Christopher Thorne
You can’t perform that action at this time.
0 commit comments