我正在导入一个 python 库,并希望创建两个具有不同参数的对象,并调用类中定义的方法。
演示.py
class Sample:
def __init__(self,path,device):
self.device=device
self.path = path
print(self.device)
print(self.path)
def getting_path(self):
print(self.path)
print(self.device)
return self.path
demo.robot
===============
*** Settings ***
*** Variables ***
${path} c:
${device} samsung
${path1} D:
${device1} samsung11
*** Test Cases ***
Test
Test_python_class
*** Keywords ***
Test_python_class
Import Library demo.Sample ${path1} ${device1}
${result} = demo.sample.getting_path
log ${result}
Import Library demo.Sample ${path} ${device}
${result1} = demo.sample.getting_path
log ${result1}
它没有创建第二个对象。${result} 和 {result1} 打印相同的值。
我可以通过使用带有两个值的 WITH 名称的以下语法来实现这一点。并使用 WITH NAME 进行如下调用
Import Library demo.Sample ${path1} ${device1} With Name c1
${result} = c1.getting_path
log ${result}
Import Library demo.Sample ${path} ${device} With Name c2
${result1} = c2.getting_path
log ${result1}
但是这个解决方案并不是最优的。如果我需要创建 10 个不同 value 的对象,我需要在这里使用 10 个 import 语句。
感谢是否有人可以提供有关最佳解决方案的任何输入,我可以将这些步骤定义为机器人函数/关键字,它将为构造函数获取这些参数并返回对象句柄,以便我们可以使用不同的对象调用类方法。