Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #109988

Re: Bulk Adding Methods Pythonically

From Random832 <random832@fastmail.com>
Newsgroups comp.lang.python
Subject Re: Bulk Adding Methods Pythonically
Date 2016-06-15 14:21 -0400
Message-ID <mailman.80.1466014920.2288.python-list@python.org> (permalink)
References <njs3nv$b19$1@dont-email.me> <1466014917.388175.638768225.2DAFD4E9@webmail.messagingengine.com>

Show all headers | View raw


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')

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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