classification
Title: `AutoNumber` class in enum documentation: support *args in constructor
Type: Stage:
Components: Documentation Versions: Python 3.8
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: docs@python Nosy List: docs@python, ethan.furman, rrt
Priority: normal Keywords:

Created on 2019-05-27 06:03 by rrt, last changed 2019-05-27 06:04 by xtreak.

Files
File name Uploaded Description Edit
foo.py rrt, 2019-05-27 06:03 Demo code
Messages (2)
msg343607 - (view) Author: Reuben Thomas (rrt) Date: 2019-05-27 06:03
By changing one line of AutoNumber:

    def __new__(cls):

to

    def __new__(cls, *args):

Enums derived from AutoNumber can now support constructors that take named arguments; for example:

class Color(AutoNumber):
    def __init__(self, pantone=None):
        self.pantone = pantone or 'unknown'

class Swatch(Color):
    AUBURN = ('3497')
    SEA_GREEN = ('1246')
    BLEACHED_CORAL = () # New color, no Pantone code yet!

Without the change, one gets the error:

TypeError: __new__() takes 1 positional argument but 2 were given

I attach runnable demonstration code.
msg343608 - (view) Author: Reuben Thomas (rrt) Date: 2019-05-27 06:03
Just to be clear, the proposed change to the documentation is simply to add ", *args" to the argument list of AutoNumber.__new__.
History
Date User Action Args
2019-05-27 06:04:29xtreaksetnosy: + ethan.furman
2019-05-27 06:03:35rrtsetmessages: + msg343608
2019-05-27 06:03:00rrtcreate