How to pass function name of child class as parameter to parent class method in python?

时间:2015-10-06 08:19:31

标签: python

from datetime import datetime
import time

class Base(object):
    def __init__(self):
        pass

    def storeResult(self, function_name, result, executed_time= None):
        pass  # store result in nested dictinary



class Derived(Base):

    def __init__(self):
         pass

    def sum(self, a, b):
        print 'In derived!'
        a = 0
        b = 0
        result = a + b
        super(Base, self).storeResult("sum", result, str(datetime.now())) # Don't want to pass string,Is there any pythonic way of passing function name

    def diff(self, a, b):
        print 'In derived!'
        result = a - b
        super(Base, self).storeResult("diff", result, str(datetime.now())) # Don't want to pass string, Is there any pythonic way of passing function name

    def multiply(self, a, b):
        print 'In derived!'
        a = 0
        b = 0
        result = a * b
        super(Base, self).storeResult("multiply", result, str(datetime.now())) # Don't want to pass string, Is there any pythonic way of passing function name

    def divide(self, a, b):
        print 'In derived!'
        a = 0
        b = 0
        result = a / b
        super(Base, self).storeResult("divide", result, str(datetime.now())) # Don't want to pass string,  Is there any pythonic way of passing function name


if __name__ == '__main__':
    d = Derived()
    d.sum(1,2)
    d.diff(2,1)
    d.multiply(1,2)
    d.divide(10,5)
    d.sum(1,12)
    d.diff(12,1)
    d.multiply(11,2)
    d.divide(10,15)
    d.sum(11,12)
    d.diff(12,1)
    d.multiply(11,2)
    d.divide(110,5)

I am facing following problem:

1) I want to call parent class method from child class: line 79, in sum :: super(Base, self).storeResult("sum", result, str(datetime.now())) AttributeError: 'super' object has no attribute 'storeResult'

2) How do you to pass function name of child class as parameter to parent class method in pythonic way?

3) I want to make sure after each function call from derived class, store each result and name of function and time of exectuted in Base class storeResult in nested dictionary like { function:{result:time}} .

I bit new to python, Thanks in advance.

1 个答案:

答案 0 :(得分:0)

Use a decorator function that will wrap called methods.

from collections import defaultdict
from datetime import datetime
from functools import wraps


def store_result(method):
    @wraps(method)
    def wrapped(self, *args, **kwargs):
        result = method(self, *args, **kwargs)
        self.storeResult(method.__name__, result, str(datetime.now()))
        return result

    return wrapped


class Base(object):
    def __init__(self):
        self.results = defaultdict(dict)

    def storeResult(self, function_name, result, executed_time= None):
        self.results[function_name][result] = executed_time


class Derived(Base):
    @store_result
    def sum(self, a, b):
        print 'In derived!'
        a = 0
        b = 0
        return a + b

    @store_result
    def diff(self, a, b):
        print 'In derived!'
        return a - b

    @store_result
    def multiply(self, a, b):
        print 'In derived!'
        a = 0
        b = 0
        return a * b

    @store_result
    def divide(self, a, b):
        print 'In derived!'
        a = 0
        b = 0
        return a / b

I've used defaultdict for storing the results - it allows passing a factory function for missing keys when assigning values.