Re: block dynamic attribute creation
On 2/19/2009 3:47 AM Bruno Desthuilliers apparently wrote: if not hasattr(self, attr) and getattr(self, '_attrlock', False): raise AttributeError(yadda yadda) # NB: assume newstyle class super(YourClass, self).__setattr__(attr, val) Thanks. Alan PS Thanks also to all who restrained themselves from saying, "don't do this". ;-) -- http://mail.python.org/mailman/listinfo/python-list
Re: block dynamic attribute creation
En Thu, 19 Feb 2009 13:20:25 -0200, Alan G Isaac escribió: if hasattr(self, attr): #update val self.__dict__[attr] = val On 2/19/2009 3:54 AM Gabriel Genellina apparently wrote: In particular, your code prevents using class attributes as a default value for instance attributes Doesn't the above allow that? Sorry, yes, sure! -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list
Re: block dynamic attribute creation
if hasattr(self, attr): #update val self.__dict__[attr] = val On 2/19/2009 3:54 AM Gabriel Genellina apparently wrote: In particular, your code prevents using class attributes as a default value for instance attributes Doesn't the above allow that? Thanks, Alan -- http://mail.python.org/mailman/listinfo/python-list
Re: block dynamic attribute creation (was: get descriptor from instance)
En Thu, 19 Feb 2009 01:29:17 -0200, Alan G Isaac escribió: OK, that's good. I'd like to sometimes lock attribute creation on instances of a class but still allow properties to function correctly. Will something like below be satisfactory? def __setattr__(self, attr, val): """If instance locked, allow no new attributes.""" try: #get the class attribute if it exists p = getattr(type(self),attr) #if it's a descriptor, use it to set val p.__set__(self, val) except AttributeError: #no descriptor if hasattr(self, attr): #update val self.__dict__[attr] = val elif getattr(self, '_attrlock', False): raise AttributeError( "Set _attrlock to False to add attributes.") else: #new attributes allowed self.__dict__[attr] = val The problem with using __setattr__ is that is slows down *all* attribute writes considerably. In particular, your code prevents using class attributes as a default value for instance attributes (not a big deal, but I like it sometimes), and you must remember to set a value for all attributes (even the "hidden" ones used by any property). But if you feel OK with that, "just do it". I think there are a few recipes in the Python Cookbook about this topic too. -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list
Re: block dynamic attribute creation
Alan G Isaac a écrit : On 2/18/2009 6:15 PM Gabriel Genellina apparently wrote: type(a).x OK, that's good. I'd like to sometimes lock attribute creation on instances of a class but still allow properties to function correctly. Will something like below be satisfactory? > def __setattr__(self, attr, val): """If instance locked, allow no new attributes.""" try: #get the class attribute if it exists p = getattr(type(self),attr) #if it's a descriptor, use it to set val p.__set__(self, val) except AttributeError: #no descriptor if hasattr(self, attr): #update val self.__dict__[attr] = val elif getattr(self, '_attrlock', False): raise AttributeError( "Set _attrlock to False to add attributes.") else: #new attributes allowed self.__dict__[attr] = val Might be safer to call on the parent class __setattr__ instead of directly assigning to the instance's dict Also, your tests in the except clause could be simplified: if not hasattr(self, attr) and getattr(self, '_attrlock', False): raise AttributeError(yadda yadda) # NB: assume newstyle class super(YourClass, self).__setattr__(attr, val) My 2 cents. -- http://mail.python.org/mailman/listinfo/python-list
Re: block dynamic attribute creation (was: get descriptor from instance)
On 2/18/2009 6:15 PM Gabriel Genellina apparently wrote: type(a).x OK, that's good. I'd like to sometimes lock attribute creation on instances of a class but still allow properties to function correctly. Will something like below be satisfactory? Thanks, Alan def __setattr__(self, attr, val): """If instance locked, allow no new attributes.""" try: #get the class attribute if it exists p = getattr(type(self),attr) #if it's a descriptor, use it to set val p.__set__(self, val) except AttributeError: #no descriptor if hasattr(self, attr): #update val self.__dict__[attr] = val elif getattr(self, '_attrlock', False): raise AttributeError( "Set _attrlock to False to add attributes.") else: #new attributes allowed self.__dict__[attr] = val -- http://mail.python.org/mailman/listinfo/python-list