By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,587 Members | 1,774 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,587 IT Pros & Developers. It's quick & easy.

Close access to the base class public methods

P: n/a
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!
Nov 11 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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
Holden Web LLC http://www.holdenweb.com/

Nov 11 '08 #2

P: n/a
On Nov 11, 9:12*pm, Steve Holden <st...@holdenweb.comwrote:
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*"?
Nov 11 '08 #3

P: n/a
RinKaMeAri wrote:
On Nov 11, 9:12 pm, Steve Holden <st...@holdenweb.comwrote:
>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/

Nov 11 '08 #4

P: n/a
On Tue, Nov 11, 2008 at 11:16 AM, RinKaMeAri <ma****@gmail.comwrote:
On Nov 11, 9:12 pm, Steve Holden <st...@holdenweb.comwrote:
>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.
<snip>
>
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=1Ms...um=3&ct=result

Cheers,
Chris
--
Follow the path of the Iguana...
http://rebertia.com
Nov 11 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.