如何在Python中向导入的类动态添加属性?
我正在编写一个新规则,使用rflint(Robotramework linter)测试我的.robot文件 但是,我需要从测试用例中提取[文档]。 源代码中尚未定义此函数,因此我为此创建了此函数:如何在Python中向导入的类动态添加属性?,python,object,oop,properties,robotframework,Python,Object,Oop,Properties,Robotframework,我正在编写一个新规则,使用rflint(Robotramework linter)测试我的.robot文件 但是,我需要从测试用例中提取[文档]。 源代码中尚未定义此函数,因此我为此创建了此函数: def doc(testcase): documentation = [] for setting in testcase.settings: if len(setting) > 2 and setting[1].lower() == "[docume
def doc(testcase):
documentation = []
for setting in testcase.settings:
if len(setting) > 2 and setting[1].lower() == "[documentation]":
for doc in setting[2:]:
if doc.startswith("#"):
# the start of a comment, so skip rest of the line
break
documentation.append(doc)
break
return str(documentation)
但是我是否可以将它作为一个属性动态添加到这个导入的测试用例中。
因此,我可以稍后将其命名为documentation=testcase.doc
class PrintDocumentation(???):
def apply(self, testcase):
documentation = testcase.doc
print(documentation)
您可能可以通过侦听器添加它,请查看