On 2/27/07, Alan Isaac <[EMAIL PROTECTED]> wrote:
> "Arnaud Delobelle" <[EMAIL PROTECTED]> wrote in message
> news:[EMAIL PROTECTED]
> > 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.
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to