1

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

3
  • 1
    « When I click on a button in the MCLidarActions....» : you mean, "in a MCLidarActions instance", I presume ? Do you want clicking on ONE button to print "Click" AND "Click UDP", that is to say the two functions _tcp_send_changed of two instances to be activated ? Or to change the consequence of clicking on your button to print "Click UDP" INSTEAD OF "Click" ? Your question is very unclear. Commented Sep 20, 2011 at 8:49
  • Print out both messages. Commented Sep 20, 2011 at 9:12
  • Thank you. I'm happy my answer being useful Commented Sep 21, 2011 at 17:13

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.