简体   繁体   中英

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

I have a module with two classes. I created the object ip = IPDB(fork=True) in the 1st class PyrouteTwo in __init__ . I have no problem to access this object in any method from the 1st class. The problem is I can't use this object in the 2nd class of the module ConfigApplier .

Here is a fragment of the code. It'll be network configurator using netlink socket to communicate with the linux kernel. The object I created is netlink communication socket and I can create just one in application, so I can't create another in the second class.

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)

Error outhput is here:

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'

Your ConfigApplier Class doesn't have the attribute self.ipr . Your other class PyrouteTwo has self.ipr but not ConfigApplier .

I'm not sure what the intent of the code was, but you need to inherit from the PyrouteTwo class as a parent. You're currently trying to use super to do that but it doesn't work that way. You could also just merge the two classes into one.

You should try inheriting from PyrouteTwo but putting it in your initial class statement here:

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

However, you could probably just add your only function in ConfigApplier to your PyrouteTwo class. So just cut your ConfigApplier function and put it in PyrouteTwo .

If you have any questions ask below.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM