1

I have a code with the same structure as bellow:

class parent(Object):

   def method1(self,name):
      raise NotImplementedError("Subclasses should implement this")


class child1(parent)

   def method1(self, name, company)
      print(name + ' ' + company)
      print(name + '!!!')
      name = 'Thanks' + name

class child2(parent)

   def method1(self, name, company)
      print(name + '----' + company)
      print(name + '!!!')
      name = 'Thanks' + name

Here I have 2 childrer that override the method of the parent. The problem is that the children classes share a code that is the same ( the second and third instructions in method1) . Is this code correct even so ? How to improve this code so that the code is not repeated?

Souad
  • 4,856
  • 15
  • 80
  • 140
  • Make an instruction in the parent and call it from the children? http://stackoverflow.com/questions/805066/call-a-parent-classs-method-from-child-class-in-python This way you only repeat the method call but share the functionality. – Totumus Maximus Feb 20 '17 at 12:54

3 Answers3

3

There is not realy a single answer. It depends on the code and what you want to do with it. In case of your sample code, i would implement an additional parent function that is templating the behavior:

class Parent(Object):
    def _method1(self, name, company, msg):
        print(name + msg + company)
        print(name + '!!!')
        name = 'Thanks' + name

    def method1(self,name):
        raise NotImplementedError("Subclasses should implement this")


class Child1(Parent)
    def method1(self, name, company)
        self._method1(name, company, '  ')


class Child2(Parent)
    def method1(self, name, company)
        self._method1(name, company, '----')

You should also use CamelCase for your class names according to the python styleguide.

1

You could build another class "in the middle" that is between the parent and the children.

This class will implement the method1.

The idea is that this class is grouping few children classes with the same functionality.

If you have few other children that need different implementation for method1, you need to define another parentGrouped class that will implement method1.

class parent(object):

  def method1(self,name):
    raise NotImplementedError("Subclasses should implement this")


class parentGrouped(parent):
  def method1(self, name, company):
    print(name + ' ' + company)
    print(name + '!!!')
    name = 'Thanks' + name

class child1(parentGrouped):
  pass



class child2(parentGrouped):
  pass
omri_saadon
  • 10,193
  • 7
  • 33
  • 58
  • @BryanOakley , Thanks for your comment, fixed that. Regarding the parent, the idea is that parent will implement methods that relevant for all children. I think that creation a class between the parent and the children is a good idea in case that we want to group behavior of some certain children. – omri_saadon Feb 20 '17 at 13:26
  • This doesn't implement the mediator pattern. It's simple inheritance. – Bryan Oakley Feb 20 '17 at 13:27
  • @BryanOakley , As i said, I've deleted the mediator pattern in my answer. – omri_saadon Feb 20 '17 at 13:28
0

You could use a mixin. Create a class that has only the definition of the function you want shared, and include that in the inheritance chain.

For example:

class Parent(object):
    def method1(self,*args, **kwargs):
        raise NotImplementedError("Subclasses should implement this")

class Method1Mixin(object):
    def method1(self, name, company):
        print(name + ' ' + company)
        print(name + '!!!')
        name = 'Thanks' + name

class child1(Method1Mixin, Parent):
    pass

class child2(Method1Mixin, Parent):
    pass

class child3(Parent):
    pass

c1 = child1()
c2 = child2()
c3 = child3()

c1.method1("bob", "bob's company")
c2.method1("carol", "carol's company")
c3.method1("ted", "ted's company")

In the above example, c1 and c2 both have an implementation of method1, whereas c3 will throw a NotImplementedError

Community
  • 1
  • 1
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685