How to make every Class Method call a specified method before execution?

Posted by norm on Stack Overflow See other posts from Stack Overflow or by norm
Published on 2010-06-08T15:50:23Z Indexed on 2010/06/08 15:52 UTC
Read the original article Hit count: 174

Filed under:

I want to make my Python Class behave in such a way that when any Class method is called a default method is executed first without explicitly specifying this in the called Class. An example may help :)

Class animals:
    def _internalMethod():
        self.respires = True

    def cat():
        self._internalMethod()
        self.name = 'cat'

    def dog():
        self._internalMethod()
        self.name = 'dog'

I want _internalMethod() to be called automatically when any method is called from an instance of animals, rather than stating it explicitly in the def of each method. Is there an elegant way to do this?

Cheers,

© Stack Overflow or respective owner

Related posts about python