1

I have a base functionality

 class Foo(object):


   def method(self):
       return True # or False based on custom logic for each class that will implement this class

   def almost_common_method(self, params, force):
      if self.method():
         params.bleh = 'foo bar'
      else:
        params.bleh = 'not foo bar'
      if force:
         foobar = some_foo_bar(model_name = 'foo bar')
       else:
         default = Default()


   def common_method(self, params, force):
     return self.almost_common_method(params, force)

So, I am thinking of making a base class... where method raises NotImplementedError I implement the common_method() in the base class as this will be shared across all the classes inheriting the base class? But is there a way to define almost_common_method() in base class.. THe variable names are common.. but it is the string assignments which will differ.. across different implementations of this base class.?

1
  • Might want to fix the indentation. Commented Aug 6, 2015 at 18:26

2 Answers 2

1

This is a good fit for the template method pattern. Break the string out into a separate method:

class FooBase(object):
    @property
    def bleh(self):
        raise NotImplementedError()
        # consider making this an abstract base class with the abc module
    def almost_common_method(self, params, force):
        params.bleh = self.bleh
        if force:
            foobar = some_foo_bar(model_name = 'foo bar')
         else:
            default = Default()

class Foo(FooBase):
    @property
    def bleh(self):
        if self.method():
            return 'foo bar'
        else:
            return 'not foo bar'

Each subclass can override bleh with its own separate implementation.

The use of @property is optional, and should generally be used only if you expect bleh to act like an attribute (that is, it doesn't change "by magic", fetching it is relatively cheap and free of visible side effects, etc.).

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

Comments

1

Define almost_common_method in the base class as well, but parameterize the values you assign using class attributes:

class Base(object):
    BLEH_IF_METHOD = 'foo bar'
    BLEH_IF_NOT_METHOD = 'not foo bar'

    def almost_common_method(self, params, force):
        if self.method():
            params.bleh = self.BLEH_IF_METHOD
        else:
            params.bleh = self.BLEAH_IF_NOT_METHOD
        if force:
            foobar = some_foo_bar(model_name = 'foo bar')
        else:
            default = Default()

Then in the child class, just override the values of the two class attributes and use the inherited method as-is.

class Child(Base):
    BLEH_IF_METHOD = 'child bleh'
    BLEH_IF_NOT_METHOD = 'not child bleh'

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.