Close access to the base class public methods
Steve Holden
steve at holdenweb.com
Tue Nov 11 14:39:53 EST 2008
RinKaMeAri wrote:
> 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*"?
It's just an expression. As in "I built A as a subclass of B, but since
I overrode all B's methods in A there was no point making it a subclass
in the first place".
regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/
More information about the Python-list
mailing list