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 Newsgroups: comp.lang.python Subject: Re: Bulk Adding Methods Pythonically Date: Wed, 15 Jun 2016 12:03:20 -0700 Lines: 27 Message-ID: References: <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: 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: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: <5761A678.8010003@stoneleaf.us> X-Mailman-Original-References: Xref: csiph.com comp.lang.python:109991 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.