【发布时间】:2015-10-06 08:19:30
【问题描述】:
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 行,总和 :: super(Base, self).storeResult("sum", result, str(datetime.now())) AttributeError: 'super' 对象没有属性 'storeResult'
2) 如何以pythonic方式将子类的函数名作为参数传递给父类方法?
3)我想确保在派生类的每个函数调用之后,将每个结果和函数名称以及在基类 storeResult 中执行的时间存储在嵌套字典中,例如 { function:{result:time}} 。
我对 python 有点陌生,在此先感谢。
【问题讨论】:
标签: python