1 回答

TA貢獻(xiàn)1797條經(jīng)驗(yàn) 獲得超4個(gè)贊
函數(shù)本身可以保存到變量中,然后調(diào)用:
f = sniffFunc f(a_packet) # Still works
被調(diào)用的函數(shù)對(duì)象sniffFunc
會(huì)像任何其他對(duì)象一樣傳遞給該函數(shù)。一旦進(jìn)入sniff
(實(shí)際上在代碼的深處),它會(huì)獲取您提供的函數(shù)對(duì)象,并通過向其傳遞給定的數(shù)據(jù)包來調(diào)用它:
def on_packet_received(self, pkt):
"""DEV: entry point. Will be called by sniff() for each
received packet (that passes the filters).
"""
if not pkt:
return
if isinstance(pkt, list):
for p in pkt:
DefaultSession.on_packet_received(self, p)
return
self.__count += 1
if self.store:
self.lst.append(pkt)
if self.prn:
result = self.prn(pkt) # Your function was stored in the variable self.prn <<<<<
if result is not None:
print(result)
result是你的函數(shù)返回的任何內(nèi)容。你可以看到它只是打印返回值(如果有的話)。
- 1 回答
- 0 關(guān)注
- 169 瀏覽
添加回答
舉報(bào)