簡體   English   中英

如何在每個模塊類中訪問對象? 蟒蛇

[英]How to access an object in every class of module? Python

我有兩個類的模塊。 我在__init__的第一個類PyrouteTwo中創建了對象ip = IPDB(fork=True) 我可以從第一類的任何方法中訪問此對象。 問題是我無法在ConfigApplier模塊的第二類中使用此對象。

這是代碼片段。 這將是使用netlink套接字與linux內核進行通信的網絡配置器。 我創建的對象是netlink通信套接字,我只能在應用程序中創建一個對象,因此無法在第二類中創建另一個對象。

class PyrouteTwo(Configurator):
    def __init__(self, inRollback=False):
        super(PyrouteTwo, self).__init__(ConfigApplier(), inRollback)
        self.runningConfig = RunningConfig()
        logging.debug("testmark.PyR2.init")
        self.ip = IPDB(fork=True)
        self.ipr = self.ip.nl

    def configureBridge(self, bridge, **opts):
        self.configApplier.addBridge(bridge)
        if bridge.port:
            bridge.port.configure(**opts)
            self.configApplier.addBridgePort(bridge)
        self.configApplier.setIfaceConfigAndUp(bridge)
        logging.debug("testmark.PyR2.confBridge..")
# !!! Here I can use the object with no problem.
        dev = self.ipr.link_lookup(ifname='em1')[0]
        logging.debug("pyroute2 link_lookup output: %d", dev)

...
class ConfigApplier(object):

    def _setIpConfig(self, iface):
        ipConfig = iface.ipConfig
        logging.debug("testmark.PyR2.ConfApplier.setIpConf.")
        if ipConfig.ipaddr:
            self.removeIpConfig(iface)
            ipwrapper.addrAdd(iface.name, ipConfig.ipaddr,
                              ipConfig.netmask)
            if ipConfig.gateway and ipConfig.defaultRoute:
                ipwrapper.routeAdd(['default', 'via', ipConfig.gateway])
# !!! But here I can't use it !!!
        dev = self.ipr.link_lookup(ifname='em1')[0]
        logging.debug("pyroute2 _setIpConfig output: %d", dev)

錯誤輸出在這里:

Traceback (most recent call last):
  File "/usr/share/vdsm/supervdsmServer", line 98, in wrapper
    res = func(*args, **kwargs)
  File "/usr/share/vdsm/supervdsmServer", line 190, in addNetwork
    return configNetwork.addNetwork(bridge, **options)
  File "/usr/share/vdsm/configNetwork.py", line 190, in wrapped
    return func(*args, **kwargs)
  File "/usr/share/vdsm/configNetwork.py", line 290, in addNetwork
    netEnt.configure(**options)
  File "/usr/share/vdsm/netmodels.py", line 159, in configure
    self.configurator.configureBridge(self, **opts)
  File "/usr/share/vdsm/netconf/pyroute_two.py", line 73, in configureBridge
    self.configApplier.setIfaceConfigAndUp(bridge)
  File "/usr/share/vdsm/netconf/pyroute_two.py", line 257, in setIfaceConfigAndUp
    self._setIpConfig(iface)
  File "/usr/share/vdsm/netconf/pyroute_two.py", line 227, in _setIpConfig
    dev = self.ipr.link_lookup(ifname='em1')[0]
AttributeError: 'ConfigApplier' object has no attribute 'ipr'

您的ConfigApplier類沒有屬性self.ipr 您的其他類PyrouteTwo具有self.ipr但沒有ConfigApplier

我不確定代碼的意圖是什么,但是您需要作為父項從PyrouteTwo類繼承。 您當前正在嘗試使用super來做到這一點,但這種方式無法正常工作。 您也可以將兩個類合並為一個。

您應該嘗試從PyrouteTwo繼承,但是將其放在您的初始類語句中:

class ConfigApplier(PyrouteTwo, object):
    #...

但是,您可能只是將ConfigApplier的唯一函數添加到PyrouteTwo類。 因此,只需剪切ConfigApplier函數並將其放在PyrouteTwoPyrouteTwo

如果您有任何疑問,請在下面詢問。

暫無
暫無

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

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