gpt4 book ai didi

Python函数调用

转载 作者:行者123 更新时间:2023-12-01 06:07:14 25 4
gpt4 key购买 nike

我有以下类(class):

from forwind.lidarapi.api import MCLidarGUIPlugin


class MCLidarActions( Handler ):

tcp_send = Event

def object__updated_changed( self, info ):
print info;
pass;

def _tcp_send_changed( self ):
print( "Click" )

from forwind.lidarapi.actions.api import MCLidarActions

class MCUDPActions( MCLidarActions ):

def object__updated_changed( self, info ):
pass;

def _tcp_send_changed( self ):
print( "Click UDP" )

当我点击MCLidarActions中的按钮时,将调用_tcp_send_changed函数,我如何扩展这个函数,我也想在MCUDPActions中采取行动。在这种情况下,如果我单击按钮,它将打印出 click 但我也想打印出 Click UDP

最佳答案

如果我正确理解你的问题,你可以这样做:

class MCLidarActions( object ):
li = []

tcp_send = 'Event'

def object__updated_changed( self, info ):
print info;
pass;

def _tcp_send_changed( self ):
print( "Click" )
for x in self.li:
x._tcp_send_changed()


class MCUDPActions( MCLidarActions ):
def __init__(self):
self.li.append(self)
def object__updated_changed( self, info ):
pass;
def _tcp_send_changed( self ):
print( "Click UDP" )


class MC_uuuuuuuuuuuuuutp_Actions( MCLidarActions ):
def __init__(self):
self.li.append(self)
def object__updated_changed( self, info ):
pass;
def _tcp_send_changed( self ):
print( "Click _uuuuuuuuuuuuuutp_" )



M = MCLidarActions()
print 'M, instance of MCLidarActions, created ------------'
print ' executing M._tcp_send_changed():'
M._tcp_send_changed()


a = MCUDPActions()
print '\na, instance of MCUDPActions, created ------------'
print ' executing M._tcp_send_changed():'
M._tcp_send_changed()
print
print ' executing a._tcp_send_changed():'
a._tcp_send_changed()


b = MCUDPActions()
print '\nb, instance of MCUDPActions, created ------------'
print ' executing M._tcp_send_changed():'
M._tcp_send_changed()
print
print ' executing a._tcp_send_changed():'
a._tcp_send_changed()
print
print ' executing b._tcp_send_changed():'
b._tcp_send_changed()


v = MC_uuuuuuuuuuuuuutp_Actions()
print '\nv, instance of MC_uuuuuuuuuuuuuutp_Actions, created ------------'
print ' executing M._tcp_send_changed():'
M._tcp_send_changed()
print
print ' executing a._tcp_send_changed():'
a._tcp_send_changed()
print
print ' executing b._tcp_send_changed():'
b._tcp_send_changed()
print
print ' executing v._tcp_send_changed():'
v._tcp_send_changed()

结果

M, instance of MCLidarActions, created ------------
executing M._tcp_send_changed():
Click

a, instance of MCUDPActions, created ------------
executing M._tcp_send_changed():
Click
Click UDP

executing a._tcp_send_changed():
Click UDP

b, instance of MCUDPActions, created ------------
executing M._tcp_send_changed():
Click
Click UDP
Click UDP

executing a._tcp_send_changed():
Click UDP

executing b._tcp_send_changed():
Click UDP

v, instance of MC_uuuuuuuuuuuuuutp_Actions, created ------------
executing M._tcp_send_changed():
Click
Click UDP
Click UDP
Click _uuuuuuuuuuuuuutp_

executing a._tcp_send_changed():
Click UDP

executing b._tcp_send_changed():
Click UDP

executing v._tcp_send_changed():
Click _uuuuuuuuuuuuuutp_

但是在上面的代码中,需要在基类的每个子类MCUDPActionsMC_uuuuuuuuuuuuuutp_Actions中定义一个函数__init__>MCLidarActions

为了避免这种情况,可以将 li 中的附加内容放在基类中:

class MCLidarActions( object ):
li = []

tcp_send = 'Event'

def __init__(self):
if self.__class__ != MCLidarActions:
self.li.append(self)

def object__updated_changed( self, info ):
print info;
pass;

def _tcp_send_changed( self ):
print( "Click" )
for x in self.li:
x._tcp_send_changed()




class MCUDPActions( MCLidarActions ):
def object__updated_changed( self, info ):
pass;
def _tcp_send_changed( self ):
print( "Click UDP" )


class MC_uuuuuuuuuuuuuutp_Actions( MCLidarActions ):
def object__updated_changed( self, info ):
pass;
def _tcp_send_changed( self ):
print( "Click _uuuuuuuuuuuuuutp_" )

结果是完全一样的。

关于Python函数调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7482092/

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