Close access to the base class public methods

R

RinKaMeAri

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!
 
S

Steve Holden

RinKaMeAri said:
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
 
R

RinKaMeAri

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


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*"?
 
S

Steve Holden

RinKaMeAri said:
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
 
C

Chris Rebert

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

Because you're inheriting from A and yet you don't want to inherit a
certain part of A, in this case public_method(), it's usually a sign
something is wrong with your class hierarchy; otherwise, you could
just inherit from something else which would have just the part of A
you want to inherit; it's a so-called "code smell", specifically
Refused Bequest I believe.

See this link into Fowler's Refactoring for more info on Refused
Bequest and the other code smells:
http://books.google.com/books?id=1M...&hl=en&sa=X&oi=book_result&resnum=3&ct=result

Cheers,
Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top