On 06/15/2016 10:37 AM, Rob Gaddi wrote:
I've got a whole lot of methods I want to add to my Channel class, all
of which following nearly the same form. The below code works, but
having to do the for loop outside of the main class definition feels
kludgey. Am I missing some cleaner answer? I thought about just
checking for all of it in __getattr__, but that's hacky too and doesn't
docstring nicely. Python 3.4
If I understand correctly:
- you are creating one class (not several similar classes)
- this class has several very similar functions
- you (understandably!) don't want to write out these very
similar functions one at a time
If that is all correct, then, as Random suggested, move that loop into
the class body and use locals() [1] to update the class dictionary.
Just make sure and delete any temporary variables.
Your other option is to put the code in a class decorator.
--
~Ethan~
[1] https://docs.python.org/3/library/functions.html#locals
Yes, returning the class namespace is a language gaurantee.
--
https://mail.python.org/mailman/listinfo/python-list