我有兩個類的模塊。 我在__init__
的第一類PyrouteTwo
中創建了對象ip = IPDB(fork=True)
。 我沒有問題從第一類的任何方法訪問此對象。 問題是我不能在模塊ConfigApplier
的第二類中使用此對象。如何訪問每個模塊類中的對象? Python
這裏是代碼片段。它將是使用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)
錯誤outhput是在這裏:
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'
不幸的是,我必須保持2個模塊的結構。這是一個很大的應用程序,我試圖添加一些功能。調用元類基地 無法創建一個一致的方法解決 順序(MRO)爲基地反對,PyrouteTwo – user1920217
嘗試更新後時出現錯誤: 當我試圖類ConfigApplier(對象,PyrouteTwo):,它迴應: 類型錯誤。如果這不起作用,只需從'ConfigApplier'完全刪除'object'繼承。 –
謝謝你的嘗試,但它沒有幫助。當我從ConfigApplier繼承中刪除'object'時,它不會運行。 – user1920217