如何在Python中找出嵌套函数的调用方

如何在Python中找出嵌套函数的调用方,python,function,testing,Python,Function,Testing,我的程序使用Telnet和SNMP与网络设备进行通信。Telnet和SNMP对于相同的功能有不同的命令,例如清除设备的配置。我需要从实际设备中提取测试逻辑,因此我使用如下硬件抽象层: #ClearConfigCommand is an interface I use in my tests def ClearCongifCommand(type = 'cli') if type == 'cli': return 'clear config' elif type =

我的程序使用Telnet和SNMP与网络设备进行通信。Telnet和SNMP对于相同的功能有不同的命令,例如清除设备的配置。我需要从实际设备中提取测试逻辑,因此我使用如下硬件抽象层:

#ClearConfigCommand is an interface I use in my tests
def ClearCongifCommand(type = 'cli')
    if type == 'cli':
        return 'clear config'
    elif type == 'snmp':
        return 'oid and some more information'
#Create connection to the device
cTelnet = Telnet('192.168.1.2')
cTelnet.Send(ClearConfigCommand())
cSNMP = SNMP('192.168.1.2')
cSNMP.Send(ClearConfigCommand('snmp'))
在我的程序中,我使用SNMP和Telnet发送命令,如下所示:

#ClearConfigCommand is an interface I use in my tests
def ClearCongifCommand(type = 'cli')
    if type == 'cli':
        return 'clear config'
    elif type == 'snmp':
        return 'oid and some more information'
#Create connection to the device
cTelnet = Telnet('192.168.1.2')
cTelnet.Send(ClearConfigCommand())
cSNMP = SNMP('192.168.1.2')
cSNMP.Send(ClearConfigCommand('snmp'))
ClearConfigCommand()是否有可能知道我正在使用的连接类型,这样我就不需要向它传递“snmp”参数?我想要的代码是:

#Create connection to the device
cTelnet = Telnet('192.168.1.2')
cSNMP = SNMP('192.168.1.2')
cTelnet.Send(ClearConfigCommand())
#We don't 
cSNMP.Send(ClearConfigCommand())

我尝试使用堆栈,但没有成功,因为ClearConfigCommand()是在Send()之前调用的,所以我无法判断哪个对象(Telnet或SNMP)正在使用ClearConfigCommand()的输出

更经典的方法是将
Telnet
SNMP
类包装成您自己的类,提供
ClearConfigCommand()


这样,除了要添加的功能外,您的类还具有原始类的所有功能。

否,函数调用表达式没有包含足够的信息,无法可靠地知道返回值或调用它的上下文将发生什么情况。感谢您的回复。这是我尝试的第一种方法。然而,事情变得更加复杂。事实上,每个Send()函数都接收3个参数1)我想发送的命令(如上所述)2)我希望接收的回复(虚拟输出)和3)指向我用来比较实际输出和虚拟输出的函数的指针。所以Send((1),(2),(3))发送一个命令(1),接收一个回复(错误消息,OK消息,特定表等),并将其与我期望的(2)使用函数(3)进行比较。如果你说,只是看不出这种方法本质上比你提出的方法更复杂。。。不管怎样,最终都必须做同样的工作。