design question: no new attributes
Chris Mellon
arkanes at gmail.com
Tue Feb 27 17:20:55 EST 2007
More information about the Python-list mailing list
Tue Feb 27 17:20:55 EST 2007
- Previous message (by thread): Pixel Array => Bitmap File
- Next message (by thread): design question: no new attributes
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On 2/27/07, Alan Isaac <aisaac at american.edu> wrote: > "Arnaud Delobelle" <arnodel at googlemail.com> wrote in message > news:1172561344.290126.191530 at h3g2000cwc.googlegroups.com... > > def __setattr__(self, attr, val): > > if hasattr(self, attr): > > self.__dict__[attr] = val > > else: > > # Tell the user off > > But then you cannot even set attributes during initialization, right? > I want that restriction only after an object is initialized. > (I realize that I could condition on an attribute set during initialization, > but I am asking for the most elegant way to achieve this.) > > Alan > You specifically excluded slots in your spec, but why? It's as close to a working, elegant solution you will find. Nothing else will work, and will be horribly inelegant. You can still do it the above way, with nasty hacking (but not really any nastier than what you're doing) by pushing the __setattr__ hook on as the last part of your __init__. Of course, if they subclass and don't call your base class __init__, then they can get by you. So maybe you should inspect the call stack in your __setattr__ and only allow instance setting if you're within your "approved" __init__ method. Even then, you can't stop them from grabbing your class, removing or overwriting the hook, and creating classes on the fly. I think at this point (actually quite a while before this point) you're getting into silly amounts of thinking about it. Python for consenting adults and all that.
- Previous message (by thread): Pixel Array => Bitmap File
- Next message (by thread): design question: no new attributes
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list