creating many similar properties
Marc 'BlackJack' Rintsch
bj_666 at gmx.net
Wed Oct 18 02:34:53 EDT 2006
In <n2fZg.3322$Ka1.1194 at news01.roc.ny>, Lee Harr wrote:
> But what if I have a whole bunch of these pwm properties?
>
> I made this:
>
> class RC(object):
> def _makeprop(name):
> prop = '_%s' % name
> def _set(self, v):
> v_new = v % 256
> setattr(self, prop, v_new)
> def _get(self):
> return getattr(self, prop)
> return property(_get, _set)
>
> pwm01 = _makeprop('pwm01')
> pwm02 = _makeprop('pwm02')
>
>
> Not too bad, except for having to repeat the name.
>
> I would like to just have a list of pwm names and
> have them all set up like that. It would be nice if
> each one was set to a default value of 127 also....
>
> Any thoughts?
Use `__getattr__()` and `__setattr__()` methods and a dictionary of names
mapped to values and another that maps the names to default values for the
modulo operation.
Ciao,
Marc 'BlackJack' Rintsch
More information about the Python-list
mailing list