5

Is there a way to force an instance method in Python to call another method prior to executing?

A simple example for demonstration:

class MyClass(object):
  def __init__(self):
    pass
  def initialize(self):
    print "I'm doing some initialization"
  def myfunc(self):
    print "This is myfunc()!"

I'd like some way to have myfunc() automatically call initialize() prior to running. If I do:

obj = MyClass()
obj.myfunc()

I'd like to see the following as the output:

I'm doing some initialization 
This is myfunc()!

Without having to explicitly call initialize() within myfunc().

The reason for this is if I have many instances of "myfunc", say myfunc1, myfunc2, myfunc3, that all need to call "initialize", I'd rather have them all call initialize once in the same place, as opposed to manually calling the method each time. Is this something meta classes could do?

blindsnowmobile
  • 3,868
  • 6
  • 32
  • 47
  • 6
    What problem are you trying to solve? Is the initialisation expensive? Why not do it in the initialiser `__init__`? – Peter Wood Jun 24 '15 at 23:40
  • 2
    Your explanation for why you don't want to simply call `initialize()` within `myfunc()` makes no sense to me. – TigerhawkT3 Jun 24 '15 at 23:46
  • I'm listening for events on a message queue (Rabbit). Different events trigger different callbacks, which correspond to methods on the class. The initialization is not expensive, but it needs to be unique each time the callback is invoked. For example, I have a unique "log_tag" value that needs to be unique for any invocation of a callback. I could just call initialize() inside every variation of myfuncN(), it just doesn't seem very DRY. – blindsnowmobile Jun 25 '15 at 00:50

3 Answers3

4

You can use a decorator in Python. Make the decorator call the function you want before the one called. You will be able to use the decorator in all your desired functions. This is syntactic sugar.

DevShark
  • 8,558
  • 9
  • 32
  • 56
2

You could make a wrapper function:

def wrapper(self, func_name):
    self.initialize()
    self.func_name()

def myfunc1(self):
    # do something

def myfunc2(self):
    # do something

obj.wrapper(myfunc1)
obj.wrapper(myfunc2)
Mike Lang
  • 96
  • 3
2

Add self.initialize() to myfunc(), this should do what you are looking for.

class MyClass(object):
    def __init__(self):
       pass
    def initialize(self):
       print("I'm doing some initialization")
    def myfunc(self):
       self.initialize()
       print("This is myfunc()!")

Try:

>>> a = MyClass()
>>> a.myfunc()
I'm doing some initialization
This is myfunc()!

Edit: However, the practice is to use decorators as suggested by DevShark.

It's discussed in another post, see Python decorators in classes

Community
  • 1
  • 1
Aziz Alto
  • 19,057
  • 5
  • 77
  • 60
  • That's what I'm wondering if I can avoid. If I have 10 or 100 "myfuncN()" methods, then I'm calling initialize() in each one. Per the post above, I think decorators are what I want. – blindsnowmobile Jun 25 '15 at 00:53