Close access to the base class public methods

RinKaMeAri maxygd at gmail.com
Tue Nov 11 14:16:46 EST 2008


On Nov 11, 9:12 pm, Steve Holden <st... at holdenweb.com> wrote:
> RinKaMeAri wrote:
> > Hi!
> > Could you imagine any way to block access to the base class public
> > methods?
> > Here is an example:
> > class B:
> >     def public_method():
> >         pass
>
> > class A(B):
> >     def public_a_method():
> >          pass
>
> > def a = A()
>
> > Is there any way to block the call a.public_method() without any
> > changes to B class?
> > Thank you!
>
> The simplest way would be to override B.public_method within A by
> defining A.public_method to raise a NotImplementedError or similar
> exception. Though of course this then begs the question of why A would
> need to subclass B in the first place, but I assume there would be
> methods you *did* want to implement.
>
> Perhaps more background would yield a better solution.
>
> regards
>  Steve
> --
> Steve Holden        +1 571 484 6266   +1 800 494 3119


Thanks, Steve! will do what you have proposed.

It is just the stub example. Have been writing some tests for class
hierarchy and found the case when user can call the base method with
the derived instance and have problems :-)  IMO, it is the class
design bug. Tried to play with __all__, setattr, getattr, but didn't
find any solution.

BTW, what do you mean "to subclass B in the *first place*"?



More information about the Python-list mailing list