Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

How to run a method before/after all class function calls with arguments passed?

class TestMeta(type):
    def __new__(mcl, name, bases, nmspc):
        def replaced_fnc(fn):
            def new_test(*args, **kwargs):
                # do whatever for before function run
                result = fn(*args, **kwargs)
                # do whatever for after function run
                return result
            return new_test
        for i in nmspc:
            if callable(nmspc[i]):
                nmspc[i] = replaced_fnc(nmspc[i])
        return (super(TestMeta, mcl).__new__(mcl, name, bases, nmspc))
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #How #run #method #class #function #calls #arguments
ADD COMMENT
Topic
Name
2+5 =