How to add a variable of a method of a class in aonther program in python?
Question: I have a class called WIFISegment as below in ns3.py :

    
    
    class WIFISegment( object ):
        """Equivalent of radio WiFi channel.
           Only Ap and WDS devices support SendFrom()."""
        def __init__( self ):
            # Helpers instantiation.
            self.channelhelper = ns.wifi.YansWifiChannelHelper.Default()
            self.phyhelper = ns.wifi.YansWifiPhyHelper.Default()
            self.wifihelper = ns.wifi.WifiHelper.Default()
            self.machelper = ns.wifi.NqosWifiMacHelper.Default()
            # Setting channel to phyhelper.
            self.channel = self.channelhelper.Create()
            self.phyhelper.SetChannel( self.channel )
    
        def add( self, node, port=None, intfName=None, mode=None ):
            """Connect Mininet node to the segment.
               Will create WifiNetDevice with Mac type specified in
               the MacHelper (default: AdhocWifiMac).
               node: Mininet node
               port: node port number (optional)
               intfName: node tap interface name (optional)
               mode: TapBridge mode (UseLocal or UseBridge) (optional)"""
            # Check if this Mininet node has assigned an underlying ns-3 node.
            if hasattr( node, 'nsNode' ) and node.nsNode is not None:
                # If it is assigned, go ahead.
                pass
            else:
                # If not, create new ns-3 node and assign it to this Mininet node.
                node.nsNode = ns.network.Node()
                allNodes.append( node )
            # Install new device to the ns-3 node, using provided helpers.
            device = self.wifihelper.Install( self.phyhelper, self.machelper, node.nsNode ).Get( 0 )
            mobilityhelper = ns.mobility.MobilityHelper()
            # Install mobility object to the ns-3 node.
            mobilityhelper.Install( node.nsNode )
            # If port number is not specified...
            if port is None:
                # ...obtain it automatically.
                port = node.newPort()
            # If interface name is not specified...
            if intfName is None:
                # ...obtain it automatically.
                intfName = Link.intfName( node, port ) # classmethod
            # In the specified Mininet node, create TBIntf bridged with the 'device'.
            tb = TBIntf( intfName, node, port, node.nsNode, device, mode )
            return tb
    

This class has a method called **def add( self, node, port=None,
intfName=None, mode=None )** and in this method we define
**_mobilityhelper_**.

I was wondering if I can use mobilityhelper in another program. For example, I
wrote another program and in my program I import WIFISegment, then define wifi
= WIFISegment() and I can use its method "add" as follow _wifi.add(h1)_ (h1 is
host here).

**My question is How I can use mobilityhelper of add() method in my other
program. Because I need to set a new mobility each time.**

Thanks Farzaneh


Answer: The obvious way is to return it:

    
    
    return tb, mobilityhelper
    

and use it like this:

    
    
    original_ret_value, your_mobilityhelper = wifi.add(h1)
    

But that would break compatibility with your old code (`add` returned
`TBIntf`, but now it returns tuple). You could add optional parameter to
indicate whether the `add` method should return mobilityhelper or not:

    
    
    def add( self, node, port=None, intfName=None, mode=None, return_mobilityhelper=False ):
        ...
        if return_mobilityhelper:
            return tb, mobilityhelper
        else:
            return tb
    

Now if you use `add` the same way you did before, it behaves the same way it
did before `wifi.add(h1)`. But you can use the new parameter and then get your
mobilityhelper

    
    
    whatever, mobilityhelper = wifi.add(h1, return_mobilityhelper=True)
    

or

    
    
    returned_tuple = wifi.add(h1, return_mobilityhelper=True)
    mobilityhelper = returned_tuple[1]
    

* * *

The other way is to modify a parameter (a list)

    
    
    def add( self, node, port=None, intfName=None, mode=None, out_mobilityhelper=None):
        ...
        if hasattr(out_mobilityhelper, "append"):  
            out_mobilityhelper.append(mobilityhelper)
        return tb
    

It's still compatible with your old code but you can pass a list to the
parameter and then extract your mobilityhelper

    
    
    mobhelp_list = []
    wifi.add(h1, out_mobilityhelper=mobhelp_list)
    mobilityhelper = mobhelp_list[0]
    

