@@ -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 |
|