Reputation: 10400
I have the following classes:
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" )
and
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" )
When I click on a button in the MCLidarActions
the _tcp_send_changed function will be called, how can I extend this function, I want to take action in the MCUDPActions as well. In this case If I click on the button it will printed out click
but I want to print out Click UDP
as well
Upvotes: 1
Views: 157
Reputation: 27575
If I understood correctly you problem, you could do:
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()
result
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_
But in the above code, it is necessary to define a function __init__ in each subclass MCUDPActions and MC_uuuuuuuuuuuuuutp_Actions of the base class MCLidarActions
To avoid that , the appending in li can be put in the base class:
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_" )
and the result is exactly the same.
Upvotes: 1
Reputation: 26968
Here is the example of calling super() Beware that you need to inherit object inorder to make super works.
class E(object):
def __init__(self):
print "enter E"
print "leave E"
class F(E):
def __init__(self):
print "enter F"
super(F, self).__init__()
print "leave F"
f = F()
Upvotes: 1