Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Random832 Newsgroups: comp.lang.python Subject: Re: Bulk Adding Methods Pythonically Date: Wed, 15 Jun 2016 14:21:57 -0400 Lines: 20 Message-ID: References: <1466014917.388175.638768225.2DAFD4E9@webmail.messagingengine.com> Mime-Version: 1.0 Content-Type: text/plain Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de EaveWQ5kWohlfvnLehti+QeHhmiVJvZHLnLnmrb3UpHQ== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'line:': 0.07; 'answer?': 0.09; 'received:internal': 0.09; 'def': 0.13; 'wed,': 0.15; '[it': 0.16; 'cleaner': 0.16; 'message-id:@webmail.messagingengine.com': 0.16; 'received:10.202': 0.16; 'received:10.202.2': 0.16; 'received:66.111': 0.16; 'received:66.111.4': 0.16; 'received:66.111.4.27': 0.16; 'received:io': 0.16; 'received:messagingengine.com': 0.16; 'received:out3-smtp.messagingengine.com': 0.16; 'received:psf.io': 0.16; 'subject:Adding': 0.16; 'wrote:': 0.16; 'class,': 0.22; 'nearly': 0.23; 'header:In-Reply-To:1': 0.24; "i've": 0.25; 'define': 0.27; 'actual': 0.28; 'dictionary': 0.29; 'loop,': 0.29; 'code': 0.30; '15,': 0.30; 'anyone': 0.32; 'class': 0.33; 'channel': 0.34; 'definition': 0.34; 'add': 0.34; 'but': 0.36; 'instead': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:10': 0.37; 'being': 0.37; 'missing': 0.37; 'things': 0.38; 'detail': 0.38; 'received:66': 0.38; 'why': 0.39; 'to:addr:python.org': 0.40; 'some': 0.40; 'header:Message-Id:1': 0.61; 'frequency': 0.66 DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=fastmail.com; h= content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-sasl-enc :x-sasl-enc; s=mesmtp; bh=5geSDFi8R6smDg/wEok5P93jaB0=; b=HHIopR 0uVo/xRfuVO51obgoUSOLWv02KlR6QmbCgf0QyOQ0TEW1WEQYqpDpkv8Ti45jFNk uTj6Vl5B5Cx7hPa0vup7lVX+iKO4iTPZNH6tRWuuJ3Yv5qv4oQTlfhQ0ashdkMwf RppPT+F+xXMOMjqmHJjPZ2cQypXYf1coSxLhg= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-sasl-enc:x-sasl-enc; s=smtpout; bh=5geSDFi8R6smDg/ wEok5P93jaB0=; b=L7n9eVX7aWEmJcRgBhxTz/9tZRDf0Bf6XqVKfePATLhRAZB ZnSk1Z2SyMFxK2Q28GSmMlXY7PUkqVBf50jIt3LPDkAC7agOGGwuh4x/SkIS4fdq eGgiGpp88X+CH3h1AE7SKqOGbREAMZxmLB0dHTVj86EuUuoa+FN+i395f8KU= X-Sasl-Enc: m0wF2eTrXcyD9yp70sIjAhdm15iAo5faxnHkUViOen+M 1466014917 X-Mailer: MessagingEngine.com Webmail Interface - ajax-aff28cd1 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: <1466014917.388175.638768225.2DAFD4E9@webmail.messagingengine.com> X-Mailman-Original-References: Xref: csiph.com comp.lang.python:109988 On Wed, Jun 15, 2016, at 13:37, 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? Inside the class definition you can add things to locals() [it returns the actual dictionary being used in class preparation], but I don't know if this can be relied on or is an implementation detail of CPython. Anyone know? But anyway, instead of using a loop, why not just define each one on its own line: def mkmeasure(fnname, measparam): ... class Channel: frequency = mkmeasure('frequency', 'FREQ') falltime = mkmeasure('falltime', 'FTIM')