74

I have a class, A, which is inherited by a bunch of other classes. Some of these have a few functions which are similar and it would be nice to have those functions defined somewhere else and called by the classes that need them. But those functions call functions defined in the super class.

class A():
    def imp_func(*args):
        # called by the child class functions

Class B(A):
    def common_func(self):
        # some stuff
        self.imp_func(*args)

So I have created my helper functions which take the self object as an argument and I can call the imp_func from inside the helper functions.

def helper_func(obj, some_args):
    # some common stuff
    obj.imp_func(*args)

class B(A):
    def common_func(self):
        # unique stuff
        helper_func(self, some_args)

This solves the problem.

But should I be doing this? Is this Pythonic?

codeforester
  • 39,467
  • 16
  • 112
  • 140
elssar
  • 5,651
  • 7
  • 46
  • 71
  • 14
    There is absolutely no problem with that. `self` is *just* a reference to the instance, it is not private nor is it dangerous to pass it to other functions. – Martijn Pieters Apr 18 '13 at 13:45
  • 4
    Why do you need to do this? `B` inherits `imp_func` from `A`, so you can call `self.imp_func(*args)` in `B.common_func` without a problem. – Florian Brucker Apr 18 '13 at 13:46
  • 1
    @FlorianBrucker There is not just one function, but a couple which are called from the parent class, and some are called a couple of times, in multiple functions in multiple classes. This leads to very bloated classes and functions. It is also time consuming to write those functions. Using helper classes will save time, effort and make the code clean and more maintainable. – elssar Apr 18 '13 at 13:50
  • 2
    I don't see how example 2 improves on example 1. In example 1, you have `B.common_func` calls `A.imp_func`. In example 2, you have `B.common_func` calls `helper_func` calls `A.imp.func`. You have **more** code to write in the second scenario. – Robᵩ Apr 18 '13 at 13:53
  • 1
    @Robᵩ That is a simplistic example to give a gist of what I'm doing, added in case my explanation was not clear – elssar Apr 18 '13 at 14:03
  • 2
    @elssar: In that case, please update your question with an example that better illustrates your problem. Right now your second example you have both more and harder to understand code, just as Robᵩ said. – Florian Brucker Apr 18 '13 at 14:44
  • You haven't given any detail so we can't see whether moving `common stuff` to `helper_func` while keeping only `unique stuff` inside B. common_func() is any better or worse. – smci Jun 07 '15 at 05:20

1 Answers1

110

There is no problem with that whatsoever - self is an object like any other and may be used in any context where object of its type/behavior would be welcome.

In Python, as exemplified by the standard library, instances of self get passed to functions (and also to methods, and even operators) all the time.

user4815162342
  • 141,790
  • 18
  • 296
  • 355