Antony Lee <[email protected]> added the comment:
I have a personal helper function for writing (Qt) GUIs that generates
ComboBoxes from Enums; essentially something like
class Choices(Enum):
choice1 = "text for choice 1"
choice2 = "text for choice 2"
def callback(choice: Choices):
# do stuff based on choice
pass
create_combobox(Choices, callback=callback)
I'm not including the actual code for create_combobox because it's not
particularly relevant here.
So far, if I wanted to add methods to the Choice enum (e.g. for simplifying the
code in `callback`), I could do it directly; if I wanted to dynamically
generate the list of choices, I could also do it (by using the functional API).
But to do both, it would have been nice to use the locals().update approach in
the bug report above. Instead, I need to do something like
class MethodsMixin(Enum):
def method(self): ...
ChoicesWithMethods = MethodsMixin("ChoicesWithMethods", [<the choices>])
(As a side note, I originally thought I could do the inheritance in the
opposite direction
Choices = Enum("Choices", [...])
class ChoicesWithMethods(Choices):
def method(self): ...
but that fails because Choices is a final class. It would be nice if it was
still possible to inherit from Choices *as long as only methods are added,
rather than new members* (I understand why adding new members is problematic).
But this is really a side point.)
Making _EnumDict actually support update() and every other relevant method is
actually not particularly difficult: I think you just need to make it inherit
from (collections.abc.MutableMapping, dict) (in that order); this is exactly
the approach used (since a while ago) by matplotlib's RcParams class
(https://github.com/matplotlib/matplotlib/blob/v3.0.0/lib/matplotlib/__init__.py#L783).
----------
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue34750>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com