1

For example we have a base class called A, and three sub classes called B C D, all inheritance of A. If I want some method only appear in B and C, but not in D. Where should I put this method?

If I put it in A, D will have the method it doesn't need.

If I put it in B and C, I repeat myself.

3
  • 1
    maybe D shouldn't inherit A? Commented Dec 15, 2015 at 4:59
  • @Dan yes, kind of new to OOP Commented Dec 15, 2015 at 6:30
  • Great, I know you already accepted, but glad you chose @Wyzard. his answer is the traditional answer to OOP that carries across all OO languages Commented Dec 15, 2015 at 14:46

4 Answers 4

2

If it's meant to be the "same" method visible in both B and C, it sounds like you need to add another class into the hierarchy below A, but above B and C. Let's call it E. You'll have D and E as subclasses of A, and B and C as subclasses of E.

Sign up to request clarification or add additional context in comments.

Comments

0

There is no 'standard' way of doing this unlike other languages.
You can override it so that it raises an exception.

class Human( object ):
    def walk( self ):
        print 'walking...'

class Baby(Human)
    def walk( self ):
        raise NotImplementedError("can't right now")

class Teenager(Human):
...

6 Comments

NotImplementedError is intended for use in abstract classes, though. Some IDE's are going to raise stink about using classes with such methods.
@Lav intended for use in abstract classes Can you provide some evidence of this?
Will Python official documentation suffice? :-) Specifically, docs.python.org/2/library/… and docs.python.org/3/library/exceptions.html#NotImplementedError .
@SaifAsif Sorry, forgot to include your name in previous comment. Anyway, I was pretty certain that using NotImplementedError in such a fashion also violates at least one PEP and did some extra research, but nope, it's legit. Still doesn't change the fact that anyone looking at your code will immediately assume that a class with NotImplementedError-raising method is an abstract class. No need to create more confusion than there already is. :-)
Works for me and makes sense
|
0

There are 2 things that you can do in your concept

  1. If all B, C and D need to ineherit from A and there is a method that should not come in D from A, then maybe you need to re-work the what methods need to go in A to avoid this.
  2. If still you cannot do any changes in A, and still D needs to inherit from A, then its best to raise the un-implemented/not-supported error like below

    class D(object):
    """docstring for D"""
    def __init__(self, arg):
        super(D, self).__init__()
        self.arg = arg
    
    def methodThatWasNotSupposedToComeHere(some_params ):
        raise NotImplementedError("This is not supported")
    

Comments

0

Python is a very flexible language in this respect.

You can define your method outside of C and D classes scope, and bind it to them separately:

class A(object):
    pass

class B(A):
    pass

def method(self):
    print '%s.method()' % type(self).__name__

class C(A):
    method = method

class D(A):
    method = method

c = C()
d = D()

c.method() # prints "C.method()"
d.method() # prints "D.method()"

print c.method # prints "<bound method C.method of <__main__.C object at 0x????????>>"
print d.method # prints "<bound method D.method of <__main__.D object at 0x????????>>"

You can introduce a new class that inherits from A, define your method in this class and inherit C and D from this new class. This is the 'classic' OOP approach.

You can implement your method in A and override it in B, telling it to raise some exception. Preferably something else than NotImplementedError as it's intended to be used in abstract classes - AttributeError seems like a better candidate. Note that if you're not going to instantiate A class in your code, it will make sense for A.method to raise NotImplementedError, effectively converting A into an abstract class.

You can resort to duck-typing and simply implement two identical methods in C and D. Python is big on duck-typing, but not big on duplicating code, so this might not be the best solution in this situation.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.