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?

3
  • 6
    What problem are you trying to solve? Is the initialisation expensive? Why not do it in the initialiser __init__? Commented Jun 24, 2015 at 23:40
  • 2
    Your explanation for why you don't want to simply call initialize() within myfunc() makes no sense to me. Commented Jun 24, 2015 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. Commented Jun 25, 2015 at 0:50

3 Answers 3

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.

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

3 Comments

Ah, didn't even think of using a decorator. I think that will do what I want... I'll give it a try.
Please give an example of how to do this, perhaps with functools.wrap
But that decorator can not be a simple method in the same class!! Isn't it?
3

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

1 Comment

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.
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)

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.