Bulk Adding Methods Pythonically
Random832
random832 at fastmail.com
Wed Jun 15 14:21:57 EDT 2016
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')
More information about the Python-list
mailing list