Author Eric.Wieser
Recipients Eric.Wieser, barry, eli.bendersky, ethan.furman
Date 2017-11-15.08:24:49
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1510734289.3.0.213398074469.issue31801@psf.upfronthosting.co.za>
In-reply-to
Content
Not necessarily an argument against this feature, but two workarounds exist for this already:


1. Use `nonlocal` to prevent `value` going into the class namespace:

    value = None
    
    class BaudRate(enum.Enum):    
        nonlocal value
        for value in rates:
            locals()['B%d' % value] = value
    
        @classmethod
        def valid_rate(cls, value):
            return (any(value == item.value for item in cls))

2. Use `types.new_class`, which is more suited to dynamic class creation anyway:

    def make_cls(ns):
        for value in rates:
            ns['B%d' % value] = value

        @classmethod
        def valid_rate(cls, value):
            return (any(value == item.value for item in cls))

        ns['valid_rate'] = valid_rate

    types.new_class('BaudRate', (enum.Enum,), exec_body=make_cls)
History
Date User Action Args
2017-11-15 08:24:49Eric.Wiesersetrecipients: + Eric.Wieser, barry, eli.bendersky, ethan.furman
2017-11-15 08:24:49Eric.Wiesersetmessageid: <1510734289.3.0.213398074469.issue31801@psf.upfronthosting.co.za>
2017-11-15 08:24:49Eric.Wieserlinkissue31801 messages
2017-11-15 08:24:49Eric.Wiesercreate