簡體   English   中英

在機器人框架工作中從不同的類調用同名的python函數

[英]calling python functions of same name from diffrent classes in robotframe work

我有一個python類,它繼承了多個類,並且test_func()具有相同的名稱

parent.py

class parent:
    def __init__(self, **kwargs):
        self.ip = kwargs.get('ip')
        self.name = kwargs.get('name')
        self.local = kwargs.get('local')

class child1(parent):
        def __init__(self,**kwargs):
            parent.__init__(self,**kwargs)

        def test_func(self):
            print 'When local =true'
            return self.name

class child2(parent):
        def __init__(self,**kwargs):
            parent.__init__(self,**kwargs)
        def test_func(self):
            print ("When local =False")
            return self.ip


class child3(parent,child1,child2):
    def __init__(self, **kwargs):
        parent.__init__(self, **kwargs)

有一個環境文件,我正在初始化類

from parent import parent
from child2 import child2
from child1 import child1

from parent import child3
server=child3(
ip = '10.20.11.10',
name ='pankaj',
local = False
)

我有一個機器人文件( Test.robot ),它調用方法test_func

*** Settings ***
Library  parent.child3  WITH NAME  wireshark_test
*** Variables ***
*** Test Cases ***
This is first
    Invoking methods
*** Keywords ***
Invoking methods
    log to console  wireshark_test.test_func

我通過命令執行此操作

pybot -V envSI.py Test.robot

我總是得到child1類的test_func

期望:

本地= 的child2child1類否則test_func test_func

有人可以指導我,我該怎么做?

我總是得到child1類的test_func

這是因為python中的多重繼承中的方法解析順序 ; 另一個閱讀是https://www.python.org/download/releases/2.3/mro/ (不要介意網址中的舊版本)。

簡而言之,當有一個多重繼承時 - 比如你的例子, class child3(parent,child1,child2)class child3(parent,child1,child2) ,當有權訪問屬性時,python將從左到右查看父類,直到找到匹配為止(和提出異常,如果沒有這樣的話)。
當您調用test_func() ,分辨率從“父”開始 - 未找到,然后轉到“child1” - 並且因為它具有此方法,所以它被拾取並執行。 因此,對於從“child3”創建的對象,它始終是“child1”中的實現。

你能改變MRO嗎? 很難(而且,你真的不想那樣做,如果你不是101%肯定你在做什么)。 您可以更改類的繼承模式; 或者您可以擁有不同的類對象 - “child1”和“child2”,並根據您的條件調用相應的對象。

在@todor的上述答案的基礎上。 我觀察到的是沒有邏輯確定如何實例化正確的類。 通常,此邏輯將駐留在某個工廠類中,但假設有一個用於確定類的簡單邏輯,則以下示例將允許您使用動態類名執行所需操作。

robot_suite.robot

*** Test Cases ***
TC - false
    Import Library    child3    
    ...    ip=10.20.11.10    
    ...    name=pankaj    
    ...    local=false    
    ...    WITH NAME    i_false

    ${result}    i_false.Test Func    
    Should Be Equal As Strings   ${result}    10.20.11.10    

TC - true
    Import Library    child3    
    ...    ip=10.20.11.10    
    ...    name=pankaj    
    ...    local=true    
    ...    WITH NAME    i_true

    ${result}    i_true.Test Func    
    Should Be Equal As Strings   ${result}    pankaj

由於庫的實例化只能執行一次,我使用WITH NAME構造來加載相同的lib兩次。

child3.py

class child3(object):

    def __init__(self, **kwargs):
        self.child = globals()["child_" + kwargs.get('local', 'true')](**kwargs)

    def get_keyword_names(self):
        return ['test func']

    def run_keyword(self, name, args):
        return getattr(self.child, name.lower().replace(' ', '_'))()


class parent:
    def __init__(self, **kwargs):
        self.ip = kwargs.get('ip', 'Default')
        self.name = kwargs.get('name', 'Default')
        self.local = kwargs.get('local', 'Default')


class child_true(parent):
        def __init__(self,**kwargs):
            parent.__init__(self,**kwargs)

        def test_func(self):
            return self.name


class child_false(parent):
        def __init__(self,**kwargs):
            parent.__init__(self,**kwargs)

        def test_func(self):
            return self.ip

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM