如何在python中将子类的函数名作为参数传递给父类方法?

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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)

我面临以下问题:

1)我想从子类调用父类方法:第79行,在sum::super(base,self).storeresult中("sum",result,str(datetime.now())attributeError:"super"对象没有属性"storeResult"

2)如何将子类的函数名作为参数传递给父类方法?

3)我要确保在派生类的每个函数调用之后,将每个结果和函数名以及在基类store result中执行的时间存储在嵌套字典中,如函数:结果:时间。

我对python有点陌生,提前谢谢。


使用将包装被调用方法的decorator函数。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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

我使用defaultdict来存储结果——它允许在分配值时为丢失的键传递一个工厂函数。