creating many similar properties

James Stroud jstroud at mbi.ucla.edu
Wed Oct 18 05:36:23 CEST 2006


Lee Harr wrote:
> I understand how to create a property like this:
> 
> class RC(object):
>     def _set_pwm(self, v):
>         self._pwm01 = v % 256
>     def _get_pwm(self):
>         return self._pwm01
>     pwm01 = property(_get_pwm, _set_pwm)
> 
> 
> 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?
> 
> Thanks for your time.

You want a "class factory". This can either be the esoteric "metaclass" 
(using "type"), which one can only understand for moments at a time, or 
something more grounded in intuition, or, a combo (for fun). You can 
probably tighten this a little, but this is the idea:

def c_factory(cname, pname, num, modulus, default):
   def accessorize(prop):
     def _get(self):
       return getattr(self, prop)
     def _set(self, v):
       v_new = v % modulus
       setattr(self, prop, v_new)
     return (_get, _set)
   def _init(self):
     for i in xrange(num):
       setattr(self, '%s%s' % (pname,i), default)
   _C = type(cname, (object,), {'__init__':_init})
   for i in xrange(num):
     prop = '_%s%s' % (pname, i)
     setattr(_C, '%s%s' % (pname, i), property(*accessorize(prop)))
   return _C

E.g.:


py> def c_factory(cname, pname, num, modulus, default):
...   def accessorize(prop):
...     def _get(self):
...       return getattr(self, prop)
...     def _set(self, v):
...       v_new = v % modulus
...       setattr(self, prop, v_new)
...     return (_get, _set)
...   def _init(self):
...     for i in xrange(num):
...       setattr(self, '%s%s' % (pname,i), default)
...   _C = type(cname, (object,), {'__init__':_init})
...   for i in xrange(num):
...     prop = '_%s%s' % (pname, i)
...     setattr(_C, '%s%s' % (pname, i), property(*accessorize(prop)))
...   return _C
...
py> Bob = c_factory('Bob', 'bob', 4, 256, 128)
py> b = Bob()
py> b.bob0
128
py> b.bob1
128
py> b.bob1 = 258
py> b.bob1
2
py> b.bob3
128
py> dir(b)

['__class__',
  '__delattr__',
  '__dict__',
  '__doc__',
  '__getattribute__',
  '__hash__',
  '__init__',
  '__module__',
  '__new__',
  '__reduce__',
  '__reduce_ex__',
  '__repr__',
  '__setattr__',
  '__str__',
  '__weakref__',
  '_bob0',
  '_bob1',
  '_bob2',
  '_bob3',
  'bob0',
  'bob1',
  'bob2',
  'bob3']
py> Bob
<class '__main__.Bob'>




-- 
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/



More information about the Python-list mailing list