【发布时间】:2019-11-19 15:30:37
【问题描述】:
我想使用 exec 或 eval 从 superclass 运行代码,但如何?
这是父类:
class A():
def f1(self):
print("hello")
这是子类:
class B(A):
def __init__(self,func):
self.funtion='super().'+func
print(self.funtion)
exec(self.funtion)
b=B('f1()')
结果
super().f1()
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-44-bdf8f567ee6e> in <module>
----> 1 b=B('f1()')
<ipython-input-43-3d6eddb1536b> in __init__(self, func)
3 self.funtion='super().'+func
4 print(self.funtion)
----> 5 exec(self.funtion)
<string> in <module>
RuntimeError: super(): no arguments
【问题讨论】:
-
您是否有理由需要使用
exec或eval? -
我需要这样的课程。
python c = Condition(data=data,func='median',separate_negative = True)和 func 在超类中定义。