Question

I'm building a class, Child, that inherits from another class, Parent. The Parent class has a loadPage method that the Child will use, except that the Child will need to run its own code near the end of the loadPage function but before the final statements of the function. I need to somehow insert this function into loadPage only for instances of Child, and not Parent. I was thinking of putting a customFunc parameter into loadPage and have it default to None for Parent, but have it default to someFunction for Child.

How do I change the defaults for the loadPage method only for instances of Child? Or am I going about this wrong? I feel like I may be overlooking a better solution.

class Parent():
    def __init__(self):
        # statement...
        # statement...
    def loadPage(self, pageTitle, customFunc=None):
        # statement...
        # statement...
        # statement...
        if customFunc:
            customFunc()
        # statement...
        # statement...

class Child(Parent):
    def __init__(self):
        Parent.__init__(self)
        self.loadPage.func_defaults = (self.someFunction)  #<-- This doesn't work
Was it helpful?

Solution

For such things, I do it in a different way :

class Parent():
   def loadPage(self, pageTitle):
      # do stuff
      self.customFunc()
      # do other stuff

   def customFunc(self):
      pass

class Child(Parent):

   def customFunc(self):
      # do the child stuff

then, a Child instance would do the stuff in customFunc while the Parent instance would do the "standard" stuff.

OTHER TIPS

Modifying your method as little as possible:

class Parent(object):
    def __init__(self):
        pass
    def loadPage(self, pageTitle, customFunc=None):
        print 'pageTitle', pageTitle
        if customFunc:
            customFunc()

class Child(Parent):
    def __init__(self):
        Parent.__init__(self)
    def loadPage(self, pagetitle, customFunc = None):
        customFunc = self.someFunction if customFunc is None else customFunc
        super(Child, self).loadPage(pagetitle, customFunc)
    def someFunction(self):
        print 'someFunction'


p = Parent()
p.loadPage('parent')
c = Child()
c.loadPage('child')

I wouldn't try to do this with defaults. Straightforward class inheritance already provides what you need.

class Parent():
    def __init__(self):
        # statement...
        # statement...

    def loadPage(self, pageTitle):
        # ... #
        self.custom_method()
        # ... #

    def custom_method(self):
        pass # or something suitably abstract

class Child(Parent):
    def __init__(self):
        Parent.__init__(self)

    def custom_method(self):    
        # what the child should do do

Can the statements before the customFunc() call be exported to a function? and the same for the statements after this call.

If yes, then the parent class will just call these two functions, and the child class will have the customFunc() call between them. So only the calls will be duplicated.

I may be overlooking a better solution.

Well, the best is probably to rely on an internal attribute, so you would have something like this:

class Parent(object):
    def __init__(self):
        self._custom_func = None
    def load_page(self, page_title):
        if self._custom_func:
            self._custom_func()

class Child(Parent):
    def __init__(self):
        super(Parent, self).__init__()
        self._load_page = some_function
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top