[英]Robot Framework keywords and Inheritance
I have a library of keywords. 我有一个关键字库。 I have a few classes and subclasses, but I'm having an issue with inheritance and keywords being double-defined.
我有一些类和子类,但是在继承和关键字被双重定义时遇到了问题。 For example:
例如:
MyLib.py MyLib.py
class Class1:
def __init__(self):
pass
def do_something_generic(self):
#do stuff that is generic to all subclasses
pass
class Subclass1(Class1):
def __init__(self):
pass
def do_something_specific_to_subclass1(self):
#something specific
pass
class Subclass2(Class1):
def __init__(self):
pass
def do_something_specific_to_subclass2(self):
#something specific
pass
The specific keywords work fine, but when I try to call Do Something Generic
I get Multiple keywords with name 'Do Something Generic' found
. 特定的关键字可以正常工作,但是当我尝试调用
Do Something Generic
我找到了Multiple keywords with name 'Do Something Generic' found
。 I can fully qualify the library name with MyLib.Class1.Do Something Generic
, but is there any way to define Do Something Generic
to always refer to the superclass, since the method is only defined there and is simply inherited by the subclasses? 我可以使用
MyLib.Class1.Do Something Generic
完全限定库名,但是有什么方法可以定义“ Do Something Generic
以始终引用超类,因为该方法仅在那里定义,并且仅由子类继承?
From Robot Framework User Guide 来自Robot Framework用户指南
When the static library API is used, Robot Framework uses reflection to find out what public methods the library class or module implements.
It will exclude all methods starting with an underscore,
and with Java libraries also methods that are implemented only in java.lang.Object are ignored.
All the methods that are not ignored are considered keywords.
Have you considered adding helper base class with a _do_something_generic
function? 您是否考虑过通过
_do_something_generic
函数添加辅助基类? You can exclude it from __all__
list. 您可以将其从
__all__
列表中排除。 Then use the inheritance to expose keywords from the base class in Class1
. 然后使用继承公开
Class1
基类的关键字。
MyLibrary.py:
__all__ = ['Class1', 'Subclass1', 'Subclass2']
class BaseClass:
def _do_something_generic(self):
pass
class Class1(BaseClass):
def __init__(self):
pass
def do_something_generic(self):
return self._do_something_generic()
class Subclass1(BaseClass):
def __init__(self):
pass
def do_something_specific_to_subclass1(self):
a = self._do_something_generic()
return (a, 3)
class Subclass2(BaseClass):
def __init__(self):
pass
def do_something_specific_to_subclass2(self):
#something specific
pass
I think the best solution is to simply move do_something_generic
to a separate class, so that your base class only has helper functions and no public keywords: 我认为最好的解决方案是将
do_something_generic
移至一个单独的类,以便您的基类仅具有辅助函数,而没有公共关键字:
class Class1:
def __init__(self):
pass
class Subclass0(Class1):
def do_something_generic(self):
#do stuff that is generic to all subclasses
pass
While it might be possible to solve this with something exotic like using __slots__
or __getattr__
, or modifying self.__dict__
, it's probably not worth the trouble. 虽然可能可以通过使用
__slots__
或__getattr__
或修改self.__dict__
之类的奇特方法来解决此问题,但这样做并不值得。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.