Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #109991
| Path | csiph.com!feeder.erje.net!2.eu.feeder.erje.net!newsreader4.netcologne.de!news.netcologne.de!newsfeed.kamp.net!newsfeed.kamp.net!fu-berlin.de!uni-berlin.de!not-for-mail |
|---|---|
| From | Ethan Furman <ethan@stoneleaf.us> |
| Newsgroups | comp.lang.python |
| Subject | Re: Bulk Adding Methods Pythonically |
| Date | Wed, 15 Jun 2016 12:03:20 -0700 |
| Lines | 27 |
| Message-ID | <mailman.81.1466017340.2288.python-list@python.org> (permalink) |
| References | <njs3nv$b19$1@dont-email.me> <5761A678.8010003@stoneleaf.us> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=windows-1252; format=flowed |
| Content-Transfer-Encoding | 7bit |
| X-Trace | news.uni-berlin.de RjEN4ZIv5/cdEB3hR3e42gvoQodpqjcSqeWl++K14YUw== |
| Return-Path | <ethan@stoneleaf.us> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.006 |
| X-Spam-Evidence | '*H*': 0.99; '*S*': 0.00; 'answer?': 0.09; 'from:addr:ethan': 0.09; 'from:addr:stoneleaf.us': 0.09; 'from:name:ethan furman': 0.09; 'message-id:@stoneleaf.us': 0.09; 'namespace': 0.09; 'python': 0.10; 'classes)': 0.16; 'cleaner': 0.16; 'dictionary.': 0.16; 'docstring': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:Adding': 0.16; 'suggested,': 0.16; 'wrote:': 0.16; 'language': 0.19; '(not': 0.20; 'class,': 0.22; 'variables.': 0.22; 'am,': 0.23; 'nearly': 0.23; 'header:In- Reply-To:1': 0.24; "i've": 0.25; 'header:User-Agent:1': 0.26; "doesn't": 0.26; 'checking': 0.27; 'correct,': 0.29; '~ethan~': 0.29; 'random': 0.29; 'creating': 0.30; 'code': 0.30; 'option': 0.31; '[1]': 0.32; 'class': 0.33; 'url:python': 0.33; 'similar': 0.33; 'channel': 0.34; 'definition': 0.34; 'add': 0.34; 'returning': 0.35; 'but': 0.36; 'too': 0.36; 'url:org': 0.36; 'url:library': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'missing': 0.37; 'thought': 0.37; 'delete': 0.38; 'several': 0.38; 'sure': 0.39; 'to:addr:python.org': 0.40; 'some': 0.40; 'url:3': 0.60; 'your': 0.60; 'body': 0.61; 'charset:windows-1252': 0.62; '3.4': 0.84; 'decorator.': 0.84; 'nicely.': 0.84; 'url:functions': 0.84 |
| User-Agent | Mozilla/5.0 (X11; Linux i686; rv:31.0) Gecko/20100101 Thunderbird/31.2.0 |
| In-Reply-To | <njs3nv$b19$1@dont-email.me> |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.22 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| X-Mailman-Original-Message-ID | <5761A678.8010003@stoneleaf.us> |
| X-Mailman-Original-References | <njs3nv$b19$1@dont-email.me> |
| Xref | csiph.com comp.lang.python:109991 |
Show key headers only | View raw
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