Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #109991
| From | Ethan Furman <ethan@stoneleaf.us> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: Bulk Adding Methods Pythonically |
| Date | 2016-06-15 12:03 -0700 |
| Message-ID | <mailman.81.1466017340.2288.python-list@python.org> (permalink) |
| References | <njs3nv$b19$1@dont-email.me> <5761A678.8010003@stoneleaf.us> |
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.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Bulk Adding Methods Pythonically Rob Gaddi <rgaddi@highlandtechnology.invalid> - 2016-06-15 17:37 +0000
Re: Bulk Adding Methods Pythonically Random832 <random832@fastmail.com> - 2016-06-15 14:21 -0400
Re: Bulk Adding Methods Pythonically Rob Gaddi <rgaddi@highlandtechnology.invalid> - 2016-06-15 18:39 +0000
Re: Bulk Adding Methods Pythonically Steven D'Aprano <steve@pearwood.info> - 2016-06-16 11:38 +1000
Re: Bulk Adding Methods Pythonically Rob Gaddi <rgaddi@highlandtechnology.invalid> - 2016-06-21 17:04 +0000
Re: Bulk Adding Methods Pythonically Ethan Furman <ethan@stoneleaf.us> - 2016-06-15 12:03 -0700
Re: Bulk Adding Methods Pythonically lists@juliensalort.org (Julien Salort) - 2016-06-16 17:53 +0200
Re: Bulk Adding Methods Pythonically Steven D'Aprano <steve@pearwood.info> - 2016-06-17 03:36 +1000
Re: Bulk Adding Methods Pythonically Lawrence D’Oliveiro <lawrencedo99@gmail.com> - 2016-06-15 18:59 -0700
Re: Bulk Adding Methods Pythonically Random832 <random832@fastmail.com> - 2016-06-16 12:15 -0400
Re: Bulk Adding Methods Pythonically Ethan Furman <ethan@stoneleaf.us> - 2016-06-16 12:58 -0700
csiph-web