Class design: accessing "private" members

Alex Martelli alex at magenta.com
Tue Jul 4 09:42:54 EDT 2000


Frédéric van der Plancke <fplancke at my-deja.com> wrote in message
news:8jqnpe$vao$1 at nnrp1.deja.com...
> In article <8jplbd0l5c at news2.newsguy.com>,
>   "Alex Martelli" <alex at magenta.com> wrote:
>
> > Such "prohibition" is not hard at all:
> >
> > class dontderive:
> >     def __init__(self):
> >         if self.__class__ != dontderive:
> >             raise "told you not to!"
> >
> > class foo(dontderive):
> >     pass
> >
>
> #but...
>
> class bypassing_prohibition(dontderive):
>    def __init__(self):
>        pass
>
> eh = bypassing_prohibition()
>
> #this works because __init__ never automatically calls the __init__
> #method of base classes...

You're perfectly right, of course.  It's easy, but it can
just as easily be fooled by the would-be-deriver.  Python
is not about programmers warring with each other, the
framework supplier trying to stop things and the framework
client finding clever workarounds; look into C++ or Java
if you want that:-).

If it were, the "if self.__class__" test might be repeated
at entry of each method of dontderive, of course:-).

This only reinforces my main contention that the best
way to stop framework-users from deriving is a line or
two to that effect in the documentation!-)


Alex






More information about the Python-list mailing list