473,403 Members | 2,071 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Inheritance but only partly?

Let's say I have a class X which has 10 methods.

I want class Y to inherit 5 of them.

Can I do that? Can I do something along the lines of super(Y, exclude
method 3 4 7 9 10) ?
Oct 2 '08 #1
12 1364
process wrote:
Let's say I have a class X which has 10 methods.

I want class Y to inherit 5 of them.

Can I do that? Can I do something along the lines of super(Y, exclude
method 3 4 7 9 10) ?

--
http://mail.python.org/mailman/listinfo/python-list
No.

But why do yo care? You can just ignore the 5 you don't want -- their
existence costs you nothing in either memory or execution speed.

You can also redefine the ones you don't want inherited:

class A:
def DontInheritMe(self, ...):
...

Class B(A):
def DontInheritMe(self, ...):
raise NotImplementedError // Or some such
Gary Herron

Oct 2 '08 #2
On Oct 2, 1:16*pm, process <circularf...@gmail.comwrote:
Let's say I have a class X which has 10 methods.

I want class Y to inherit 5 of them.

Can I do that? Can I do something along the lines of super(Y, exclude
method 3 4 7 9 10) ?
I think the noral way of doing that is to split the origional class
into two classes.

What you have:

class BaseClass(object):

def method0:
pass

def method1:
pass

def method2:
pass

...

def method9:
pass

What you need:
class BaseClassWant(object):
def method0:
pass

def method1:
pass

...

def method4:
pass

class BaseClassDontWant(object):
def method5:
pass

def method6:
pass

...

def method9:
pass

class BaseClass(BaseClassWant, BaseClassDontWant): # same as BaseClass
above
pass

class YourClass(BaseClassWant):
pass
Matt
Oct 2 '08 #3
On Oct 2, 3:16*pm, process <circularf...@gmail.comwrote:
Let's say I have a class X which has 10 methods.

I want class Y to inherit 5 of them.

Can I do that? Can I do something along the lines of super(Y, exclude
method 3 4 7 9 10) ?
That implies that the 5 you do include don't rely on or call the 5 you
don't. Otherwise you have to inherit them. Then you can refactor
them into:

class X5YouDo: ...
class X5YouDont: ...
class X( X5YouDo, X5YouDont ): ...
class Y( X5YouDo ): ...

If you're looking for restricted visibility, Python does not have it.
It's just handcuffs, and makes things you can't do.

After all, nothing would stop you user from calling:

y= Y()
X5YouDont.DontInheritMe( y, args )

to get at the uninherited methods.
Oct 2 '08 #4
Gary Herron wrote:
process wrote:
>Let's say I have a class X which has 10 methods.

I want class Y to inherit 5 of them.

Can I do that? Can I do something along the lines of super(Y, exclude
method 3 4 7 9 10) ?

--
http://mail.python.org/mailman/listinfo/python-list

No.

But why do yo care? You can just ignore the 5 you don't want -- their
existence costs you nothing in either memory or execution speed.

You can also redefine the ones you don't want inherited:

class A:
def DontInheritMe(self, ...):
...

Class B(A):
def DontInheritMe(self, ...):
raise NotImplementedError // Or some such
Gary Herron

--
http://mail.python.org/mailman/listinfo/python-list
Here's another (very Python) possibility

class Base:
class m1(self, ...):
...
class m2(self, ...):
...

class NotInheritable:
class m3(self, ...):
...

class A(Base, NotInheritable);
...

class B(Base):
...

Oct 2 '08 #5
Gary Herron:
You can also redefine the ones you don't want inherited:
class A:
def DontInheritMe(self, ...):
...
Class B(A):
def DontInheritMe(self, ...):
raise NotImplementedError // Or some such
I have never used something like this, but the OP may use a masking
class too:

class A(object):
def m1(self): pass
def m2(self): pass
def m3(self): pass
def m4(self): pass

class Mask(object):
def m3(self): raise NotImplementedError
def m4(self): raise NotImplementedError

class B(Mask, A):
pass

a = A()
a.m1()
a.m2()
a.m3()
a.m4()

b = B()
b.m1()
b.m2()
b.m3() # raises
b.m4() # raises

In a language without multiple inheritance you need a different trick,
I presume.
What's the name of this python design pattern? :-)

Bye,
bearophile
Oct 2 '08 #6
be************@lycos.com wrote:
class Mask(object):
def m3(self): raise NotImplementedError
def m4(self): raise NotImplementedError
What's the name of this python design pattern? :-)
Don't know. Perhaps we could call it the FigLeaf pattern
(covering up what you don't want seen)?

There's another possibility that's even more pythonish
(I won't say pythonic, since it's not necessarily
a *recommended* thing to do):

class A:
m1 = B.__dict__['m1']
m2 = B.__dict__['m2']
...

I propose calling this the Magpie pattern (stealing
the shiny baubles you want and hiding them away in
your own nest).

--
Greg
Oct 3 '08 #7
greg wrote:
>class Mask(object):
def m3(self): raise NotImplementedError
def m4(self): raise NotImplementedError
>What's the name of this python design pattern? :-)

Don't know. Perhaps we could call it the FigLeaf pattern
(covering up what you don't want seen)?
Braghettone ;)
Oct 3 '08 #8
On Oct 2, 10:16*pm, process <circularf...@gmail.comwrote:
Let's say I have a class X which has 10 methods.

I want class Y to inherit 5 of them.

Can I do that? Can I do something along the lines of super(Y, exclude
method 3 4 7 9 10) ?
Don't use inheritance, use delegation or just copy the methods you
need:

class A(object):
def meth_a(self):
pass

class B(object):
meth_a = A.meth_a.im_func
IMO, if you have methods that you want to use in different classes,
this is hint that
you are in need of generic functions. See this blog post for an
example:

http://www.artima.com/weblogs/viewpo...?thread=237764
Oct 3 '08 #9
2008/10/2 process <ci**********@gmail.com>:
Let's say I have a class X which has 10 methods.

I want class Y to inherit 5 of them.

Can I do that?
As others have said, no. What nobody seems to have said yet is why. If
Y descends from X, you are saying that Y is an X; that a Y can be used
anywhere an X can. If Y doesn't support some methods of X then it is
*not* an X, and *can't* be used anywhere an X can.

Rather than looking at workarounds, as others have, I think you need
to go back to your design and work out what's gone wrong that you
/want/ to descend Y from X. Your present design is wrong, plain and
simple. Working around it won't fix that.

--
Tim Rowe
Oct 3 '08 #10
2008/10/3 Tim Rowe <di*****@gmail.com>:
As others have said, no. What nobody seems to have said yet is why. If
Y descends from X, you are saying that Y is an X; that a Y can be used
anywhere an X can. If Y doesn't support some methods of X then it is
*not* an X, and *can't* be used anywhere an X can.
See <http://en.wikipedia.org/wiki/Liskov_substitution_principle>.

--
Cheers,
Simon B.
si***@brunningonline.net
http://www.brunningonline.net/simon/blog/
GTalk: simon.brunning | MSN: small_values | Yahoo: smallvalues |
Twitter: brunns | Facebook: http://tinyurl.com/6f47zo
Oct 3 '08 #11
On Fri, 03 Oct 2008 03:32:52 -0700, Michele Simionato wrote:
IMO, if you have methods that you want to use in different classes, this
is hint that
you are in need of generic functions. See this blog post for an example:

http://www.artima.com/weblogs/viewpo...?thread=237764

That's a very interesting article, but I'm afraid I don't understand what
makes them "generic functions" as opposed to just functions. Your simple
generic example:
from pkgutil import simplegeneric

@simplegeneric
def print_out(self, text, *args):
if args:
text = text % args
print >self.stdout, text
# and similar for print_err and readln_in

class FileOut(object):
def __init__(self):
self.stdout = file('out.txt', 'w')

print_out(FileOut(), 'writing on file') # prints a line on out.txt

doesn't seem to do anything extra that the following would do:
def print_out2(obj, text, *args):
if args:
text = text % args
print >obj.stdout, text

class FileOut2(object):
def __init__(self):
self.stdout = file('out2.txt', 'w')

print_out(FileOut2(), 'writing on file')

What's the difference?
--
Steven
Oct 4 '08 #12
On Oct 3, 11:56*pm, Steven D'Aprano <st...@REMOVE-THIS-
cybersource.com.auwrote:
On Fri, 03 Oct 2008 03:32:52 -0700, Michele Simionato wrote:
IMO, if you have methods that you want to use in different classes, this
is hint that
you are in need of generic functions. See this blog post for an example:
http://www.artima.com/weblogs/viewpo...?thread=237764

That's a very interesting article, but I'm afraid I don't understand what
makes them "generic functions" as opposed to just functions. Your simple
generic example:

from pkgutil import simplegeneric

@simplegeneric
def print_out(self, text, *args):
* * if args:
* * * * text = text % args
* * print >self.stdout, text
# and similar for print_err and readln_in

class FileOut(object):
* * def __init__(self):
* * * * self.stdout = file('out.txt', 'w')

print_out(FileOut(), 'writing on file') # prints a line on out.txt

doesn't seem to do anything extra that the following would do:

def print_out2(obj, text, *args):
* * if args:
* * * * text = text % args
* * print >obj.stdout, text

class FileOut2(object):
* * def __init__(self):
* * * * self.stdout = file('out2.txt', 'w')

print_out(FileOut2(), 'writing on file')

What's the difference?

--
Steven
Did you read the next section, "extending generic functions" ?

George
Oct 5 '08 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: km | last post by:
Hi all, In the following code why am i not able to access class A's object attribute - 'a' ? I wishto extent class D with all the attributes of its base classes. how do i do that ? thanks in...
14
by: Steve Jorgensen | last post by:
Recently, I tried and did a poor job explaining an idea I've had for handling a particular case of implementation inheritance that would be easy and obvious in a fully OOP language, but is not at...
45
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using...
14
by: Bruno van Dooren | last post by:
Hi all, i am having a problems with inheritance. consider the following: class A { public: A(int i){;} };
11
by: RickHodder | last post by:
I'm having a problem, and here is a simplified example of code that demonstrates it: public class BizObj { public string TableName=""; private DataSet oData; public BizObj()
60
by: Shawnk | last post by:
Some Sr. colleges and I have had an on going discussion relative to when and if C# will ever support 'true' multiple inheritance. Relevant to this, I wanted to query the C# community (the...
49
by: Ben Voigt [C++ MVP] | last post by:
I'm trying to construct a compelling example of the need for a language feature, with full support for generics, to introduce all static members and nested classes of another type into the current...
21
by: raylopez99 | last post by:
Well, contrary to the implication in my 2000 textbook on C# (public beta version), C# does allow multiple inheritance, so long as it's serially chained as follows: class derived02 : derived01 {...
6
by: karthikbalaguru | last post by:
Hi, Could someone here tell me some links/pdfs/tutorials to know about the difference between Private Inheritance and Public Inheritance ? I am unable to get info w.r.t it. Thx in advans,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.