gpt4 book ai didi

python - 如何在python中的另一个程序中添加一个类的方法的变量?

转载 作者:太空宇宙 更新时间:2023-11-04 00:55:09 25 4
gpt4 key购买 nike

我在 ns3.py 中有一个名为 WIFISegment 的类:

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

此类有一个名为 def add( self, node, port=None, intfName=None, mode=None ) 的方法,在这个方法中我们定义了 mobilityhelper

我想知道我是否可以在另一个程序中使用 mobilityhelper。例如,我编写了另一个程序,在我的程序中导入 WIFISegment,然后定义 wifi = WIFISegment() 并且我可以使用它的方法“添加”,如下所示 wifi.add(h1)(h1 是主机这里)。

我的问题是如何在我的其他程序中使用 add() 方法的 mobilityhelper。因为我每次都需要设置一个新的mobility。

谢谢法扎内

最佳答案

最明显的方法是返回它:

return tb, mobilityhelper

并像这样使用它:

original_ret_value, your_mobilityhelper = wifi.add(h1)

但这会破坏与旧代码的兼容性(add 返回 TBIntf,但现在它返回元组)。您可以添加可选参数以指示 add 方法是否应返回 mobilityhelper:

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

现在,如果您像以前一样使用 add,它的行为方式与 wifi.add(h1) 之前的方式相同。但是您可以使用新参数,然后获取您的 mobilityhelper

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

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

另一种方式是修改一个参数(一个列表)

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

它仍然与您的旧代码兼容,但您可以将列表传递给参数,然后提取您的 mobilityhelper

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

关于python - 如何在python中的另一个程序中添加一个类的方法的变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35450860/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com