Re: creating many similar properties

2006-10-19 Thread Michele Simionato
James Stroud wrote: > However, I think that what you are saying about metaclasses being > brittle relates more to implementation than language. In theory, these > should be equivalent: > > (1) class Bob(object): pass > (2) Bob = type('Bob', (), {}) > > And indeed a cursory inspection of the

Re: creating many similar properties

2006-10-19 Thread Michele Simionato
Carl Banks wrote: > You sound as if you're avoiding metaclasses just for the sake of > avoiding them, which is just as bad as using them for the sake of using > them. Do you realize that you are effectively saying "avoiding a complex tool in favor of a simpler one is just as bad as avoing the simp

Re: creating many similar properties

2006-10-18 Thread James Stroud
Michele Simionato wrote: > George Sakkis wrote: > >>Why is this less hidden or magical than a metaclass ? > > > Because it does not use inheritance. It is not going to create > properties on subclasses without > you noticing it. Also, metaclasses are brittle: try to use them with > __slots__, or

Re: creating many similar properties

2006-10-18 Thread Carl Banks
Michele Simionato wrote: > Carl Banks wrote: > > Come on, I don't think anyone's under the impression we're being > > indiscriminate here. > > Ok, but I don't think that in the case at hand we should recommend a > metaclass > solution. You sound as if you're avoiding metaclasses just for the sake

Re: creating many similar properties

2006-10-18 Thread Michele Simionato
Carl Banks wrote: > Come on, I don't think anyone's under the impression we're being > indiscriminate here. Ok, but I don't think that in the case at hand we should recommend a metaclass solution. Michele Simionato -- http://mail.python.org/mailman/listinfo/python-list

Re: creating many similar properties

2006-10-18 Thread Carl Banks
Michele Simionato wrote: > Carl Banks wrote: > > Devil's Advocate: he did say "hidden magic TO YOUR CLASS". > > > > If you use a (real) metaclass, then you have the icky feeling of a > > class permanently tainted by the unclean metaclass (even though the > > metaclass does nothing other than touch

Re: creating many similar properties

2006-10-18 Thread Carl Banks
George Sakkis wrote: > There's a subtle common bug here: all _get and _set closures will refer > to the last property only. You have to remember to write "def > _set(self,v,prop=prop)" and similarly for _get to do the right thing. Sorry. My mistake. > By the way, I can't think of a case where t

Re: creating many similar properties

2006-10-18 Thread Michele Simionato
Carl Banks wrote: > Devil's Advocate: he did say "hidden magic TO YOUR CLASS". > > If you use a (real) metaclass, then you have the icky feeling of a > class permanently tainted by the unclean metaclass (even though the > metaclass does nothing other than touch the class dict upon creation); > wher

Re: creating many similar properties

2006-10-18 Thread Carl Banks
George Sakkis wrote: > Michele Simionato wrote: > > import sys > > > > def defprop(name, default=127): > > loc = sys._getframe(1).f_locals > > prop = '_%s' % name > > def _set(self, v): > > v_new = v % 256 > > setattr(self, prop, v_new) > > def _get(self): > >

Re: creating many similar properties

2006-10-18 Thread Michele Simionato
George Sakkis wrote: > > from itertools import chain, izip, repeat > > def ByteProperties(*names, **defaulted_names): > def byte_property(name, default): > return property(lambda self: getattr(self, name, default), > lambda self,v: setattr(self, name, v%256)) >

Re: creating many similar properties

2006-10-18 Thread Michele Simionato
George Sakkis wrote: > > Why is this less hidden or magical than a metaclass ? Because it does not use inheritance. It is not going to create properties on subclasses without you noticing it. Also, metaclasses are brittle: try to use them with __slots__, or with non-standard classes (i.e. extensio

Re: creating many similar properties

2006-10-18 Thread George Sakkis
Carl Banks wrote: > 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

Re: creating many similar properties

2006-10-18 Thread George Sakkis
Michele Simionato wrote: > 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) > > > >

Re: creating many similar properties

2006-10-18 Thread Carl Banks
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 pw

Re: creating many similar properties

2006-10-17 Thread Michele Simionato
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

Re: creating many similar properties

2006-10-17 Thread Marc 'BlackJack' Rintsch
In <[EMAIL PROTECTED]>, 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)

Re: creating many similar properties

2006-10-17 Thread James Stroud
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

creating many similar properties

2006-10-17 Thread Lee Harr
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