I'm implementing an enum class and want to retrieve an aggregate information about the enum members. Each enum represents a breakout board with different number of pins, and I want to get the maximum number of pins over all boards. My approach was to add a class attribute _max_pins which is adjusted during __init__ of each member.
Problem is, it is not possible to define _max_pins ahead if the members as it would become a member, too. It does not help to define it after the members as then the members cannot access it during their __init__
I've looked at Declare a static variable in an enum class but the table can be set up after __init__ - that would be possible but would need to scan again all members after their initialization.
class IgelTyp(Enum):
LED_1 = (24, 1)
LED_2 = (24, 2)
LED_3 = (16, 4)
_max_pin = -1
def __init__(self, pins, groups):
if _max_pin < pins//groups: # gives error
_max_pin = pins//groups
@classmethod
def get_max_pins(cls):
return cls._max_pin
Above code produces UnboundLocalError: local variable '_max_pin' referenced before assignment
When I move the assignment of _max_pin in front of the member definition it tells me that TypeError: __init__() missing 2 required positional arguments: ...
Edit 1
Actually, the TypeError is raised regardless where I put the assignment within the class.
And when I use IgelTyp._max_pin = -1 I get a NameError: name 'IgelTyp' is not defined
Anyone has an efficient and readable solution?
IgelTyp._max_pininstead.__init__method.self._max_pin...NameErroris given.